DLLEXPORT void jl_yield() { static jl_function_t *yieldfunc = NULL; if (yieldfunc == NULL) yieldfunc = (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("yield")); if (yieldfunc != NULL && jl_is_func(yieldfunc)) jl_call0(yieldfunc); }
void jl_add_standard_imports(jl_module_t *m) { // using Base jl_module_using(m, jl_base_module); // importall Base.Operators jl_module_importall(m, (jl_module_t*)jl_get_global(jl_base_module, jl_symbol("Operators"))); }
static jl_module_t *eval_import_path_(jl_array_t *args, int retrying) { // in .A.B.C, first find a binding for A in the chain of module scopes // following parent links. then evaluate the rest of the path from there. // in A.B, look for A in Main first. jl_sym_t *var = (jl_sym_t*)jl_cellref(args,0); size_t i=1; assert(jl_is_symbol(var)); jl_module_t *m; if (var != dot_sym) { m = jl_main_module; } else { m = jl_current_module; while (1) { var = (jl_sym_t*)jl_cellref(args,i); i++; if (var != dot_sym) break; m = m->parent; } } while (1) { if (jl_binding_resolved_p(m, var)) { jl_binding_t *mb = jl_get_binding(m, var); assert(mb != NULL); if (mb->value == NULL || !jl_is_module(mb->value)) jl_errorf("invalid module path"); m = (jl_module_t*)mb->value; break; } if (m == jl_main_module) { if (!retrying) { if (require_func == NULL && jl_base_module != NULL) require_func = jl_get_global(jl_base_module, jl_symbol("require")); if (require_func != NULL) { jl_value_t *str = jl_cstr_to_string(var->name); JL_GC_PUSH1(&str); jl_apply((jl_function_t*)require_func, &str, 1); JL_GC_POP(); return eval_import_path_(args, 1); } } } jl_errorf("in module path: %s not defined", var->name); } for(; i < jl_array_len(args)-1; i++) { jl_value_t *s = jl_cellref(args,i); assert(jl_is_symbol(s)); m = (jl_module_t*)jl_eval_global_var(m, (jl_sym_t*)s); if (!jl_is_module(m)) jl_errorf("invalid import statement"); } return m; }
int true_main(int argc, char *argv[]) { if (jl_base_module != NULL) { jl_array_t *args = jl_alloc_cell_1d(argc); jl_set_global(jl_base_module, jl_symbol("ARGS"), (jl_value_t*)args); int i; for (i=0; i < argc; i++) { jl_arrayset(args, (jl_value_t*)jl_cstr_to_string(argv[i]), i); } } jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"), jl_cstr_to_string(julia_home)); jl_module_export(jl_core_module, jl_symbol("JULIA_HOME")); // run program if specified, otherwise enter REPL if (program) { return exec_program(); } init_repl_environment(argc, argv); jl_function_t *start_client = (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("_start")); if (start_client) { jl_apply(start_client, NULL, 0); return 0; } // client event loop not available; use fallback blocking version int iserr = 0; again: ; JL_TRY { if (iserr) { jl_show(jl_stderr_obj(), jl_exception_in_transit); ios_printf(ios_stderr, "\n\n"); iserr = 0; } while (1) { char *input = read_expr("julia> "); if (!input || ios_eof(ios_stdin)) { ios_printf(ios_stdout, "\n"); break; } jl_value_t *ast = jl_parse_input_line(input); jl_value_t *value = jl_toplevel_eval(ast); jl_show(jl_stdout_obj(), value); ios_printf(ios_stdout, "\n\n"); } } JL_CATCH { iserr = 1; goto again; } return 0; }
jl_array_t *jl_get_loaded_modules(void) { static jl_value_t *loaded_modules_array = NULL; if (loaded_modules_array == NULL && jl_base_module != NULL) loaded_modules_array = jl_get_global(jl_base_module, jl_symbol("loaded_modules_array")); if (loaded_modules_array != NULL) return (jl_array_t*)jl_call0((jl_function_t*)loaded_modules_array); return NULL; }
static void jl_uv_call_close_callback(jl_value_t *val) { jl_value_t *args[2]; args[0] = jl_get_global(jl_base_relative_to(((jl_datatype_t*)jl_typeof(val))->name->module), jl_symbol("_uv_hook_close")); // topmod(typeof(val))._uv_hook_close args[1] = val; assert(args[0]); jl_apply(args, 2); }
// return char* from String field in Base.GIT_VERSION_INFO static const char *git_info_string(const char *fld) { static jl_value_t *GIT_VERSION_INFO = NULL; if (!GIT_VERSION_INFO) GIT_VERSION_INFO = jl_get_global(jl_base_module, jl_symbol("GIT_VERSION_INFO")); jl_value_t *f = jl_get_field(GIT_VERSION_INFO, fld); assert(jl_is_string(f)); return jl_string_data(f); }
void jl_atexit_hook() { if (jl_base_module) { jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("_atexit")); if (f!=NULL && jl_is_function(f)) { jl_apply((jl_function_t*)f, NULL, 0); } } }
void jl_show(jl_value_t *v) { if (jl_system_module) { if (jl_show_gf == NULL) { jl_show_gf = (jl_function_t*)jl_get_global(jl_system_module, jl_symbol("show")); } jl_apply(jl_show_gf, &v, 1); } }
DLLEXPORT void jl_save_system_image(char *fname, char *startscriptname) { jl_gc_collect(); jl_gc_collect(); int en = jl_gc_is_enabled(); jl_gc_disable(); htable_reset(&backref_table, 50000); ios_t f; ios_file(&f, fname, 1, 1, 1, 1); // orphan old Base module if present jl_base_module = (jl_module_t*)jl_get_global(jl_main_module, jl_symbol("Base")); // delete cached slow ASCIIString constructor if present jl_methtable_t *mt = jl_gf_mtable((jl_function_t*)jl_ascii_string_type); jl_array_t *spec = mt->defs->func->linfo->specializations; if (spec->length > 0 && ((jl_lambda_info_t*)jl_cellref(spec,0))->inferred == 0) { mt->cache = JL_NULL; mt->cache_arg1 = JL_NULL; mt->defs->func->linfo->tfunc = (jl_value_t*)jl_null; mt->defs->func->linfo->specializations = NULL; } jl_idtable_type = jl_get_global(jl_base_module, jl_symbol("ObjectIdDict")); jl_serialize_value(&f, jl_array_type->env); jl_serialize_value(&f, jl_main_module); write_int32(&f, jl_get_t_uid_ctr()); write_int32(&f, jl_get_gs_ctr()); htable_reset(&backref_table, 0); ios_t ss; ios_file(&ss, startscriptname, 1, 0, 0, 0); ios_copyall(&f, &ss); ios_close(&ss); ios_putc(0, &f); ios_close(&f); if (en) jl_gc_enable(); }
int true_main(int argc, char *argv[]) { if (lisp_prompt) { jl_lisp_prompt(); return 0; } jl_array_t *args = jl_alloc_cell_1d(argc); jl_set_global(jl_system_module, jl_symbol("ARGS"), (jl_value_t*)args); int i; for (i=0; i < argc; i++) { jl_arrayset(args, i, (jl_value_t*)jl_cstr_to_string(argv[i])); } jl_set_const(jl_system_module, jl_symbol("JULIA_HOME"), jl_cstr_to_string(julia_home)); // run program if specified, otherwise enter REPL if (program) { return exec_program(); } init_repl_environment(); have_color = detect_color(); char *prompt = have_color ? jl_prompt_color : jl_prompt_plain; prompt_length = strlen(jl_prompt_plain); prompt_string = prompt; jl_function_t *start_client = (jl_function_t*) jl_get_global(jl_system_module, jl_symbol("_start")); if (start_client == NULL) { repl_print_prompt(); // client event loop not available; use fallback blocking version int iserr = 0; again: ; JL_TRY { if (iserr) { if (have_color) { ios_printf(ios_stdout, jl_color_normal); } jl_show(jl_exception_in_transit); ios_printf(ios_stdout, "\n\n"); iserr = 0; } while (1) { read_expr(prompt); } } JL_CATCH { iserr = 1; goto again; } }
jl_function_t *jl_module_call_func(jl_module_t *m) { if (m->call_func == NULL) { jl_function_t *cf = (jl_function_t*)jl_get_global(m, call_sym); if (cf == NULL || !jl_is_function(cf)) cf = jl_bottom_func; m->call_func = cf; } return m->call_func; }
void jl_register_root_module(jl_value_t *key, jl_module_t *m) { static jl_value_t *register_module_func = NULL; assert(jl_base_module); if (register_module_func == NULL) register_module_func = jl_get_global(jl_base_module, jl_symbol("register_root_module")); assert(register_module_func); jl_value_t *rmargs[3] = {register_module_func, key, (jl_value_t*)m}; jl_apply(rmargs, 3); }
void jl_show(jl_value_t *stream, jl_value_t *v) { if (jl_base_module) { if (jl_show_gf == NULL) { jl_show_gf = (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("show")); } jl_value_t *args[2] = {stream,v}; jl_apply(jl_show_gf, args, 2); } }
void jl_add_standard_imports(jl_module_t *m) { assert(jl_base_module != NULL); // using Base jl_module_using(m, jl_base_module); // importall Base.Operators jl_module_t *opmod = (jl_module_t*)jl_get_global(jl_base_module, jl_symbol("Operators")); if (opmod != NULL) jl_module_importall(m, opmod); }
// handle a command line input event void handle_input(jl_value_t *ast, int end, int show_value) { if (end) { show_value = -1; ast = jl_nothing; } jl_value_t *f = jl_get_global(jl_base_module,jl_symbol("repl_callback")); assert(f); jl_value_t *fargs[] = { ast, jl_box_long(show_value) }; jl_apply((jl_function_t*)f, fargs, 2); }
static jl_value_t* call_jl_function_with_string(const char *fname, const char *arg, size_t arglen) { jl_value_t *f = jl_get_global(jl_base_module,jl_symbol(fname)); assert(f); jl_value_t **fargs; JL_GC_PUSHARGS(fargs, 1); fargs[0] = jl_pchar_to_string((char*)arg, arglen); jl_value_t *result = jl_apply((jl_function_t*)f, fargs, 1); JL_GC_POP(); return result; }
int true_main(int argc, char *argv[]) { if (jl_base_module != NULL) { jl_array_t *args = jl_alloc_cell_1d(argc); jl_set_global(jl_base_module, jl_symbol("ARGS"), (jl_value_t*)args); int i; for (i=0; i < argc; i++) { jl_arrayset(args, (jl_value_t*)jl_cstr_to_string(argv[i]), i); } } // run program if specified, otherwise enter REPL if (program) { int ret = exec_program(); uv_tty_reset_mode(); return ret; } jl_function_t *start_client = (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("_start")); //uv_read_start(jl_stdin_tty,jl_alloc_read_buffer,&read_buffer); if (start_client) { jl_apply(start_client, NULL, 0); //rl_cleanup_after_signal(); return 0; } // client event loop not available; use fallback blocking version //install_read_event_handler(&echoBack); int iserr = 0; again: ; JL_TRY { if (iserr) { //jl_show(jl_exception_in_transit);# What if the error was in show? jl_printf(JL_STDERR, "\n\n"); iserr = 0; } uv_run(jl_global_event_loop(),UV_RUN_DEFAULT); } JL_CATCH { iserr = 1; JL_PUTS("error during run:\n",JL_STDERR); jl_show(jl_stderr_obj(),jl_exception_in_transit); JL_PUTS("\n",JL_STDOUT); goto again; } uv_tty_reset_mode(); return iserr; }
void jl_register_root_module(jl_module_t *m) { static jl_value_t *register_module_func = NULL; assert(jl_base_module); if (register_module_func == NULL) register_module_func = jl_get_global(jl_base_module, jl_symbol("register_root_module")); assert(register_module_func); jl_value_t *args[2]; args[0] = register_module_func; args[1] = (jl_value_t*)m; jl_apply(args, 2); }
CXX_WRAP_EXPORT jl_datatype_t* julia_type(const std::string& name) { for(jl_module_t* mod : {jl_base_module, g_cxx_wrap_module, jl_current_module}) { jl_value_t* gval = jl_get_global(mod, jl_symbol(name.c_str())); if(gval != nullptr && jl_is_datatype(gval)) { return (jl_datatype_t*)gval; } } throw std::runtime_error("Symbol for type " + name + " was not found"); }
static jl_value_t* repl_parse_input_line(char *buf) { if (buf[0] == ';') { buf++; jl_value_t *f = jl_get_global(jl_base_module,jl_symbol("repl_hook")); assert(f); jl_value_t **fargs; JL_GC_PUSHARGS(fargs, 1); fargs[0] = jl_pchar_to_string((char*)buf, strlen(buf)); jl_value_t *result = jl_apply((jl_function_t*)f, fargs, 1); JL_GC_POP(); return result; } return jl_parse_input_line(buf); }
JuliaFunction::JuliaFunction(const std::string& name, const std::string& module_name) { jl_module_t* mod = module_name.empty() ? jl_current_module : (jl_module_t*)jl_get_global(jl_current_module, jl_symbol(module_name.c_str())); if(mod == nullptr) { throw std::runtime_error("Could not find module " + module_name + " when looking up function " + module_name); } m_function = jl_get_function(mod, name.c_str()); if(m_function == nullptr) { throw std::runtime_error("Could not find function " + name); } }
// handle a command line input event void handle_input(jl_value_t *ast, int end, int show_value) { if (end) { show_value = -1; ast = jl_nothing; } jl_value_t *f = jl_get_global(jl_base_module,jl_symbol("repl_callback")); assert(f); jl_value_t **fargs; JL_GC_PUSHARGS(fargs, 2); fargs[0] = ast; fargs[1] = jl_box_long(show_value); jl_apply((jl_function_t*)f, fargs, 2); JL_GC_POP(); }
CXX_WRAP_EXPORT jl_function_t* julia_function(const std::string& name, const std::string& module_name) { jl_module_t* mod = module_name.empty() ? jl_current_module : (jl_module_t*)jl_get_global(jl_current_module, jl_symbol(module_name.c_str())); if(mod == nullptr) { throw std::runtime_error("Could not find module " + module_name + " when looking up function " + module_name); } jl_function_t* f = jl_get_function(mod, name.c_str()); if(f == nullptr) { throw std::runtime_error("Could not find function " + name); } return f; }
void jl_show(jl_value_t *stream, jl_value_t *v) { if (jl_base_module) { if (jl_show_gf == NULL) { jl_show_gf = (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("show")); } if (jl_show_gf==NULL || stream==NULL) { JL_PRINTF(JL_STDERR, " could not show value of type %s", jl_is_tuple(v) ? "Tuple" : ((jl_datatype_t*)jl_typeof(v))->name->name->name); return; } jl_value_t *args[2] = {stream,v}; jl_apply(jl_show_gf, args, 2); } }
DLLEXPORT void uv_atexit_hook() { #if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS) jl_print_gc_stats(JL_STDERR); #endif if (jl_base_module) { jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("_atexit")); if (f!=NULL && jl_is_function(f)) { JL_TRY { jl_apply((jl_function_t*)f, NULL, 0); } JL_CATCH { JL_PRINTF(JL_STDERR, "\natexit hook threw an error: "); jl_show(jl_stderr_obj(),jl_exception_in_transit); } } }
void jl_depwarn(const char *msg, jl_value_t *sym) { static jl_value_t *depwarn_func = NULL; if (!depwarn_func && jl_base_module) { depwarn_func = jl_get_global(jl_base_module, jl_symbol("depwarn")); } if (!depwarn_func) { jl_safe_printf("WARNING: %s\n", msg); return; } jl_value_t **depwarn_args; JL_GC_PUSHARGS(depwarn_args, 3); depwarn_args[0] = depwarn_func; depwarn_args[1] = jl_cstr_to_string(msg); depwarn_args[2] = sym; jl_apply(depwarn_args, 3); JL_GC_POP(); }
JL_DLLEXPORT void jl_depwarn_partial_indexing(size_t n) { static jl_value_t *depwarn_func = NULL; if (!depwarn_func && jl_base_module) { depwarn_func = jl_get_global(jl_base_module, jl_symbol("_depwarn_for_trailing_indices")); } if (!depwarn_func) { jl_safe_printf("WARNING: omitting indices for non-singleton trailing dimensions is deprecated. Use " "`reshape(A, Val(%zd))` or add trailing `1` indices to make the dimensionality of the array match " "the number of indices\n", n); return; } jl_value_t **depwarn_args; JL_GC_PUSHARGS(depwarn_args, 2); depwarn_args[0] = depwarn_func; depwarn_args[1] = jl_box_long(n); jl_apply(depwarn_args, 2); JL_GC_POP(); }
static void print_obj_profile(void) { jl_value_t *errstream = jl_get_global(jl_base_module, jl_symbol("stderr_stream")); JL_TRY { if (errstream) jl_set_current_output_stream_obj(errstream); ios_t *s = jl_current_output_stream(); for(int i=0; i < obj_counts.size; i+=2) { if (obj_counts.table[i+1] != HT_NOTFOUND) { ios_printf(s, "%d ", obj_counts.table[i+1]-1); jl_show(obj_counts.table[i]); ios_printf(s, "\n"); } } } JL_CATCH { } }
JL_DLLEXPORT void jl_set_ARGS(int argc, char **argv) { if (jl_core_module != NULL) { jl_array_t *args = (jl_array_t*)jl_get_global(jl_core_module, jl_symbol("ARGS")); if (args == NULL) { args = jl_alloc_vec_any(0); JL_GC_PUSH1(&args); jl_set_const(jl_core_module, jl_symbol("ARGS"), (jl_value_t*)args); JL_GC_POP(); } assert(jl_array_len(args) == 0); jl_array_grow_end(args, argc); int i; for (i=0; i < argc; i++) { jl_value_t *s = (jl_value_t*)jl_cstr_to_string(argv[i]); jl_arrayset(args, s, i); } } }