mirror of
https://github.com/janet-lang/janet
synced 2025-05-02 07:24:15 +00:00
Add janet_loop_fiber C function to run a fiber to completion from C.
This is mainly meant for use as the entry point to a C wrapper for a janet program. This maeans the programmer doesn't need to use an ifdef to handle if the event loop is enabled.
This commit is contained in:
parent
aea1f59f6e
commit
55b8563c08
@ -108,3 +108,19 @@ int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Jan
|
|||||||
return janet_dobytes(env, (const uint8_t *)str, len, sourcePath, out);
|
return janet_dobytes(env, (const uint8_t *)str, len, sourcePath, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Run a fiber to completion (use event loop if enabled). Return the status. */
|
||||||
|
int janet_loop_fiber(JanetFiber *fiber) {
|
||||||
|
int status;
|
||||||
|
#ifdef JANET_EV
|
||||||
|
janet_schedule(fiber, janet_wrap_nil());
|
||||||
|
janet_loop();
|
||||||
|
status = janet_fiber_status(fiber);
|
||||||
|
#else
|
||||||
|
Janet out;
|
||||||
|
status = janet_continue(fiber, janet_wrap_nil(), &out);
|
||||||
|
if (status != JANET_SIGNAL_OK && status != JANET_SIGNAL_EVENT) {
|
||||||
|
janet_stacktrace(fiber, out);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
@ -1424,9 +1424,13 @@ JANET_API JanetCompileResult janet_compile_lint(
|
|||||||
JANET_API JanetTable *janet_core_env(JanetTable *replacements);
|
JANET_API JanetTable *janet_core_env(JanetTable *replacements);
|
||||||
JANET_API JanetTable *janet_core_lookup_table(JanetTable *replacements);
|
JANET_API JanetTable *janet_core_lookup_table(JanetTable *replacements);
|
||||||
|
|
||||||
|
/* Execute strings */
|
||||||
JANET_API int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out);
|
JANET_API int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out);
|
||||||
JANET_API int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out);
|
JANET_API int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out);
|
||||||
|
|
||||||
|
/* Run the entrypoint of a wrapped program */
|
||||||
|
JANET_API int janet_loop_fiber(JanetFiber *fiber);
|
||||||
|
|
||||||
/* Number scanning */
|
/* Number scanning */
|
||||||
JANET_API int janet_scan_number(const uint8_t *str, int32_t len, double *out);
|
JANET_API int janet_scan_number(const uint8_t *str, int32_t len, double *out);
|
||||||
JANET_API int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out);
|
JANET_API int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out);
|
||||||
|
@ -1021,7 +1021,6 @@ int main(int argc, char **argv) {
|
|||||||
janet_init_hash_key(hash_key);
|
janet_init_hash_key(hash_key);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* Set up VM */
|
/* Set up VM */
|
||||||
janet_init();
|
janet_init();
|
||||||
|
|
||||||
@ -1048,18 +1047,8 @@ int main(int argc, char **argv) {
|
|||||||
JanetFiber *fiber = janet_fiber(janet_unwrap_function(mainfun), 64, 1, mainargs);
|
JanetFiber *fiber = janet_fiber(janet_unwrap_function(mainfun), 64, 1, mainargs);
|
||||||
fiber->env = env;
|
fiber->env = env;
|
||||||
|
|
||||||
#ifdef JANET_EV
|
/* Run the fiber in an event loop */
|
||||||
janet_gcroot(janet_wrap_fiber(fiber));
|
status = janet_loop_fiber(fiber);
|
||||||
janet_schedule(fiber, janet_wrap_nil());
|
|
||||||
janet_loop();
|
|
||||||
status = janet_fiber_status(fiber);
|
|
||||||
#else
|
|
||||||
Janet out;
|
|
||||||
status = janet_continue(fiber, janet_wrap_nil(), &out);
|
|
||||||
if (status != JANET_SIGNAL_OK && status != JANET_SIGNAL_EVENT) {
|
|
||||||
janet_stacktrace(fiber, out);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Deinitialize vm */
|
/* Deinitialize vm */
|
||||||
janet_deinit();
|
janet_deinit();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user