void jl_load(const char *fname) { char *fpath = (char*)fname; struct stat stbuf; if (jl_stat(fpath, (char*)&stbuf) != 0) { jl_errorf("could not open file %s", fpath); } jl_start_parsing_file(fpath); jl_parse_eval_all(fpath); if (fpath != fname) free(fpath); }
static inline jl_value_t *jl_intrinsic_cvt(jl_value_t *ty, jl_value_t *a, const char *name, intrinsic_cvt_t op, intrinsic_cvt_check_t check_op) { jl_ptls_t ptls = jl_get_ptls_states(); jl_value_t *aty = jl_typeof(a); if (!jl_is_bitstype(aty)) jl_errorf("%s: value is not a bitstype", name); if (!jl_is_bitstype(ty)) jl_errorf("%s: type is not a bitstype", name); void *pa = jl_data_ptr(a); unsigned isize = jl_datatype_size(aty); unsigned osize = jl_datatype_size(ty); if (check_op && check_op(isize, osize, pa)) jl_throw(jl_inexact_exception); jl_value_t *newv = jl_gc_alloc(ptls, ((jl_datatype_t*)ty)->size, ty); op(aty == (jl_value_t*)jl_bool_type ? 1 : isize * host_char_bit, pa, osize * host_char_bit, jl_data_ptr(newv)); if (ty == (jl_value_t*)jl_bool_type) return *(uint8_t*)jl_data_ptr(newv) & 1 ? jl_true : jl_false; return newv; }
jl_value_t *jl_load(const char *fname, size_t len) { if (jl_current_module->istopmod) { jl_printf(JL_STDOUT, "%s\r\n", fname); #ifdef _OS_WINDOWS_ uv_run(uv_default_loop(), (uv_run_mode)1); #endif } char *fpath = (char*)fname; uv_stat_t stbuf; if (jl_stat(fpath, (char*)&stbuf) != 0 || (stbuf.st_mode & S_IFMT) != S_IFREG) { jl_errorf("could not open file %s", fpath); } if (jl_start_parsing_file(fpath) != 0) { jl_errorf("could not open file %s", fpath); } jl_value_t *result = jl_parse_eval_all(fpath, len); if (fpath != fname) free(fpath); return result; }
static void allocate_segv_handler(void) { struct sigaction act; memset(&act, 0, sizeof(struct sigaction)); sigemptyset(&act.sa_mask); act.sa_sigaction = segv_handler; act.sa_flags = SA_ONSTACK | SA_SIGINFO; if (sigaction(SIGSEGV, &act, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } }
extern "C" DLLEXPORT void *jl_load_and_lookup(char *f_lib, char *f_name, uv_lib_t **hnd) { uv_lib_t *handle = *hnd; if (!handle) *hnd = handle = get_library(f_lib); void *ptr = jl_dlsym_e(handle, f_name); if (!ptr) jl_errorf("symbol could not be found %s: %s\n", f_name, uv_dlerror(handle)); return ptr; }
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. jl_sym_t *var = (jl_sym_t*)jl_cellref(args,0); assert(jl_is_symbol(var)); jl_module_t *m = jl_current_module; while (1) { jl_binding_t *mb = jl_get_binding(m, var); if (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_PUSH(&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); } m = m->parent; } for(size_t i=1; 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 jl_field_index(jl_datatype_t *t, jl_sym_t *fld, int err) { jl_tuple_t *fn = t->names; for(size_t i=0; i < jl_tuple_len(fn); i++) { if (jl_tupleref(fn,i) == (jl_value_t*)fld) { return (int)i; } } if (err) jl_errorf("type %s has no field %s", t->name->name->name, fld->name); return -1; }
jl_value_t *jl_load(const char *fname) { if (jl_current_module == jl_base_module) { //This deliberatly uses ios, because stdio initialization has been moved to Julia jl_printf(JL_STDOUT, "%s\r\n", fname); #ifdef _OS_WINDOWS_ uv_run(uv_default_loop(), (uv_run_mode)1); #endif } char *fpath = (char*)fname; uv_stat_t stbuf; if (jl_stat(fpath, (char*)&stbuf) != 0 || (stbuf.st_mode & S_IFMT) != S_IFREG) { jl_errorf("could not open file %s", fpath); } if (jl_start_parsing_file(fpath) != 0) { jl_errorf("could not open file %s", fpath); } jl_value_t *result = jl_parse_eval_all(fpath); if (fpath != fname) free(fpath); return result; }
static void NORETURN jl_dlerror(const char *fmt, const char *sym) { #ifdef _OS_WINDOWS_ CHAR reason[256]; FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), reason, sizeof(reason) / sizeof(reason[0]), NULL); #else const char *reason = dlerror(); #endif jl_errorf(fmt, sym, reason); }
static jl_module_t *eval_import_path(jl_array_t *args) { jl_module_t *m = jl_root_module; for(size_t i=0; i < args->length-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; }
void jl_set_tag_type_super(jl_tag_type_t *tt, jl_value_t *super) { if (!jl_is_tag_type(super) || super == (jl_value_t*)jl_undef_type || jl_subtype(super,(jl_value_t*)jl_type_type,0)) { jl_errorf("invalid subtyping in definition of %s",tt->name->name->name); } tt->super = (jl_tag_type_t*)super; if (jl_tuple_len(tt->parameters) > 0) { tt->name->cache = jl_null; jl_reinstantiate_inner_types((jl_tag_type_t*)tt); } }
static void *alloc_sigstack(size_t size) { size_t pagesz = jl_getpagesize(); // Add one guard page to catch stack overflow in the signal handler size = LLT_ALIGN(size, pagesz) + pagesz; void *stackbuff = mmap(0, size, PROT_READ | PROT_WRITE, MAP_NORESERVE | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (stackbuff == MAP_FAILED) jl_errorf("fatal error allocating signal stack: mmap: %s", strerror(errno)); mprotect(stackbuff, pagesz, PROT_NONE); return (void*)((char*)stackbuff + pagesz); }
// empty generic function def // TODO: maybe have jl_method_def call this DLLEXPORT jl_value_t *jl_generic_function_def(jl_sym_t *name, jl_value_t **bp, jl_value_t *bp_owner, jl_binding_t *bnd) { jl_value_t *gf=NULL; if (bnd && bnd->value != NULL && !bnd->constp) jl_errorf("cannot define function %s; it already has a value", bnd->name->name); if (*bp != NULL) { gf = *bp; if (!jl_is_gf(gf)) jl_errorf("cannot define function %s; it already has a value", name->name); } if (bnd) bnd->constp = 1; if (*bp == NULL) { jl_module_t *module = (bnd ? bnd->owner : NULL); gf = (jl_value_t*)jl_new_generic_function(name, module); *bp = gf; if (bp_owner) jl_gc_wb(bp_owner, gf); } return gf; }
JL_DLLEXPORT int jl_field_index(jl_datatype_t *t, jl_sym_t *fld, int err) { jl_svec_t *fn = t->name->names; for(size_t i=0; i < jl_svec_len(fn); i++) { if (jl_svecref(fn,i) == (jl_value_t*)fld) { return (int)i; } } if (err) jl_errorf("type %s has no field %s", jl_symbol_name(t->name->name), jl_symbol_name(fld)); return -1; }
void jl_load(const char *fname) { char *fpath = jl_find_file_in_path(fname); jl_value_t *ast = jl_parse_file(fpath); if (ast == (jl_value_t*)jl_null) { if (fpath != fname) free(fpath); jl_errorf("could not open file %s", fpath); } JL_GC_PUSH(&ast); jl_load_file_expr(fpath, ast); JL_GC_POP(); if (fpath != fname) free(fpath); }
static size_t field_offset(jl_struct_type_t *t, jl_sym_t *fld, int err) { jl_tuple_t *fn = t->names; size_t i; for(i=0; i < jl_tuple_len(fn); i++) { if (jl_tupleref(fn,i) == (jl_value_t*)fld) { return i; } } if (err) jl_errorf("type %s has no field %s", t->name->name->name, fld->name); return -1; }
DLLEXPORT void jl_checked_assignment(jl_binding_t *b, jl_value_t *rhs) { if (b->constp && b->value != NULL) { if (!jl_egal(rhs, b->value)) { if (jl_typeof(rhs) != jl_typeof(b->value) || jl_is_type(rhs) || jl_is_function(rhs) || jl_is_module(rhs)) { jl_errorf("invalid redefinition of constant %s", b->name->name); } JL_PRINTF(JL_STDERR,"Warning: redefining constant %s\n",b->name->name); } } b->value = rhs; }
void jl_binding_deprecation_warning(jl_binding_t *b) { if (b->deprecated && jl_options.depwarn) { if (jl_options.depwarn != JL_OPTIONS_DEPWARN_ERROR) jl_printf(JL_STDERR, "WARNING: "); if (b->owner) jl_printf(JL_STDERR, "%s.%s is deprecated", jl_symbol_name(b->owner->name), jl_symbol_name(b->name)); else jl_printf(JL_STDERR, "%s is deprecated", jl_symbol_name(b->name)); jl_value_t *v = b->value; if (v && (jl_is_type(v) || (jl_is_function(v) && jl_is_gf(v)))) { jl_printf(JL_STDERR, ", use "); if (b->owner && strcmp(jl_symbol_name(b->owner->name), "Base") == 0 && strcmp(jl_symbol_name(b->name), "Uint") == 0) { // TODO: Suggesting type b->value is wrong for typealiases. // Uncommon in Base, hardcoded here for now, see #13221 jl_printf(JL_STDERR, "UInt"); } else { jl_static_show(JL_STDERR, v); } jl_printf(JL_STDERR, " instead"); } jl_printf(JL_STDERR, ".\n"); if (jl_options.depwarn != JL_OPTIONS_DEPWARN_ERROR) jl_printf(JL_STDERR, " likely near %s:%d\n", jl_filename, jl_lineno); if (jl_options.depwarn == JL_OPTIONS_DEPWARN_ERROR) { if (b->owner) jl_errorf("deprecated binding: %s.%s", jl_symbol_name(b->owner->name), jl_symbol_name(b->name)); else jl_errorf("deprecated binding: %s", jl_symbol_name(b->name)); } } }
jl_value_t *jl_eval_module_expr(jl_expr_t *ex, int *plineno) { assert(ex->head == module_sym); jl_module_t *last_module = jl_current_module; jl_sym_t *name = (jl_sym_t*)jl_exprarg(ex, 0); if (!jl_is_symbol(name)) { jl_type_error("module", (jl_value_t*)jl_sym_type, (jl_value_t*)name); } if (name == jl_current_module->name) { jl_errorf("module name %s conflicts with enclosing module", name->name); } jl_binding_t *b = jl_get_binding_wr(jl_current_module, name); jl_declare_constant(b); if (b->value != NULL) { JL_PRINTF(JL_STDERR, "Warning: redefinition of module %s ignored\n", name->name); return jl_nothing; } jl_module_t *newm = jl_new_module(name); b->value = (jl_value_t*)newm; if (jl_current_module == jl_core_module && name == jl_symbol("Base")) { // pick up Base module during bootstrap, and stay within it // after loading. jl_base_module = last_module = newm; } JL_GC_PUSH(&last_module); jl_current_module = newm; jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 1))->args; JL_TRY { for(int i=0; i < exprs->length; i++) { // process toplevel form jl_value_t *form = jl_cellref(exprs, i); if (jl_is_linenode(form)) { if (plineno) *plineno = jl_linenode_line(form); } else { (void)jl_toplevel_eval_flex(form, 0, plineno); } } } JL_CATCH { JL_GC_POP(); jl_current_module = last_module; jl_raise(jl_exception_in_transit); } JL_GC_POP(); jl_current_module = last_module; return jl_nothing; }
static inline jl_value_t *jl_fintrinsic_1(jl_value_t *ty, jl_value_t *a, const char *name, fintrinsic_op1 *floatop, fintrinsic_op1 *doubleop) { if (!jl_is_bitstype(jl_typeof(a))) jl_errorf("%s: value is not a bitstype", name); if (!jl_is_bitstype(ty)) jl_errorf("%s: type is not a bitstype", name); jl_value_t *newv = newstruct((jl_datatype_t*)ty); void *pa = jl_data_ptr(a), *pr = jl_data_ptr(newv); unsigned sz = jl_datatype_size(jl_typeof(a)); unsigned sz2 = jl_datatype_size(ty); switch (sz) { /* choose the right size c-type operation based on the input */ case 4: floatop(sz2 * host_char_bit, pa, pr); break; case 8: doubleop(sz2 * host_char_bit, pa, pr); break; default: jl_errorf("%s: runtime floating point intrinsics are not implemented for bit sizes other than 32 and 64", name); } return newv; }
// get binding for adding a method // like jl_get_binding_wr, but uses existing imports instead of warning // and overwriting. jl_binding_t *jl_get_binding_for_method_def(jl_module_t *m, jl_sym_t *var) { jl_binding_t **bp = (jl_binding_t**)ptrhash_bp(&m->bindings, var); jl_binding_t *b = *bp; if (b != HT_NOTFOUND) { if (b->owner != m && b->owner != NULL) { jl_binding_t *b2 = jl_get_binding(b->owner, var); if (b2 == NULL) jl_errorf("invalid method definition: imported function %s.%s does not exist", b->owner->name->name, var->name); if (!b->imported) jl_errorf("error in method definition: function %s.%s must be explicitly imported to be extended", b->owner->name->name, var->name); return b2; } b->owner = m; return b; } b = new_binding(var); b->owner = m; *bp = b; return *bp; }
void jl_set_datatype_super(jl_datatype_t *tt, jl_value_t *super) { if (!jl_is_datatype(super) || !jl_is_abstracttype(super) || tt->name == ((jl_datatype_t*)super)->name || jl_subtype(super,(jl_value_t*)jl_vararg_type,0) || jl_is_tuple_type(super) || jl_subtype(super,(jl_value_t*)jl_type_type,0) || super == (jl_value_t*)jl_builtin_type) { jl_errorf("invalid subtyping in definition of %s", jl_symbol_name(tt->name->name)); } tt->super = (jl_datatype_t*)super; jl_gc_wb(tt, tt->super); }
static inline jl_value_t *jl_iintrinsic_2(jl_value_t *a, jl_value_t *b, const char *name, char (*getsign)(void*, unsigned), jl_value_t *(*lambda2)(jl_value_t*, void*, void*, unsigned, unsigned, const void*), const void *list, int cvtb) { jl_value_t *ty = jl_typeof(a); jl_value_t *tyb = jl_typeof(b); if (tyb != ty) { if (!cvtb) jl_errorf("%s: types of a and b must match", name); if (!jl_is_bitstype(tyb)) jl_errorf("%s: b is not a bitstypes", name); } if (!jl_is_bitstype(ty)) jl_errorf("%s: a is not a bitstypes", name); void *pa = jl_data_ptr(a), *pb = jl_data_ptr(b); unsigned sz = jl_datatype_size(ty); unsigned sz2 = next_power_of_two(sz); unsigned szb = jl_datatype_size(tyb); if (sz2 > sz) { /* round type up to the appropriate c-type and set/clear the unused bits */ void *pa2 = alloca(sz2); memcpy(pa2, pa, sz); memset((char*)pa2 + sz, getsign(pa, sz), sz2 - sz); pa = pa2; } if (sz2 > szb) { /* round type up to the appropriate c-type and set/clear/truncate the unused bits */ void *pb2 = alloca(sz2); memcpy(pb2, pb, szb); memset((char*)pb2 + szb, getsign(pb, sz), sz2 - szb); pb = pb2; } jl_value_t *newv = lambda2(ty, pa, pb, sz, sz2, list); return newv; }
void jl_set_datatype_super(jl_datatype_t *tt, jl_value_t *super) { if (!jl_is_datatype(super) || !jl_is_abstracttype(super) || tt->name == ((jl_datatype_t*)super)->name || jl_subtype(super,(jl_value_t*)jl_vararg_type,0) || jl_subtype(super,(jl_value_t*)jl_type_type,0)) { jl_errorf("invalid subtyping in definition of %s",tt->name->name->name); } tt->super = (jl_datatype_t*)super; gc_wb(tt, tt->super); if (jl_tuple_len(tt->parameters) > 0) { tt->name->cache = (jl_value_t*)jl_null; jl_reinstantiate_inner_types(tt); } }
static size_t field_offset(jl_struct_type_t *t, jl_sym_t *fld, int err) { jl_tuple_t *fn = t->names; size_t i; for(i=0; i < fn->length; i++) { if (jl_tupleref(fn,i) == (jl_value_t*)fld) { if (t == jl_struct_kind || t == jl_bits_kind || t == jl_tag_kind) i += 3; return i; } } if (err) jl_errorf("type %s has no field %s", t->name->name->name, fld->name); return -1; }
void *jl_install_thread_signal_handler(void) { void *signal_stack = alloc_sigstack(sig_stack_size); stack_t ss; ss.ss_flags = 0; ss.ss_size = sig_stack_size; ss.ss_sp = signal_stack; if (sigaltstack(&ss, NULL) < 0) { jl_errorf("fatal error: sigaltstack: %s", strerror(errno)); } #if !defined(HAVE_MACH) struct sigaction act; memset(&act, 0, sizeof(struct sigaction)); sigemptyset(&act.sa_mask); act.sa_sigaction = usr2_handler; act.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTART; if (sigaction(SIGUSR2, &act, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } #endif return signal_stack; }
void *jl_dlsym(void *handle, char *symbol) { #ifndef WIN32 (void)dlerror(); void *ptr = dlsym(handle, symbol); char *msg = dlerror(); if (msg != NULL) { jl_errorf("dlsym: %s", msg); } return ptr; #else void *ptr = GET_FUNCTION_FROM_MODULE(handle, symbol); #endif return ptr; }
void jl_install_default_signal_handlers(void) { struct sigaction actf; memset(&actf, 0, sizeof(struct sigaction)); sigemptyset(&actf.sa_mask); actf.sa_sigaction = fpe_handler; actf.sa_flags = SA_SIGINFO; if (sigaction(SIGFPE, &actf, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { jl_error("fatal error: Couldn't set SIGPIPE"); } if (signal(SIGTRAP, SIG_IGN) == SIG_ERR) { jl_error("fatal error: Couldn't set SIGTRAP"); } allocate_segv_handler(); struct sigaction act_die; memset(&act_die, 0, sizeof(struct sigaction)); sigemptyset(&act_die.sa_mask); act_die.sa_sigaction = sigdie_handler; act_die.sa_flags = SA_SIGINFO; if (sigaction(SIGILL, &act_die, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } if (sigaction(SIGABRT, &act_die, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } if (sigaction(SIGSYS, &act_die, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } // need to ensure the following signals are not SIG_IGN, even though they will be blocked act_die.sa_flags = SA_SIGINFO | SA_RESTART; #if defined(HAVE_ITIMER) if (sigaction(SIGPROF, &act_die, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } #endif #ifdef SIGINFO if (sigaction(SIGINFO, &act_die, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } #else if (sigaction(SIGUSR1, &act_die, NULL) < 0) { jl_errorf("fatal error: sigaction: %s", strerror(errno)); } #endif }
JL_DLLEXPORT void jl_checked_assignment(jl_binding_t *b, jl_value_t *rhs) { if (b->constp && b->value != NULL) { if (!jl_egal(rhs, b->value)) { if (jl_typeof(rhs) != jl_typeof(b->value) || jl_is_type(rhs) || jl_is_function(rhs) || jl_is_module(rhs)) { jl_errorf("invalid redefinition of constant %s", jl_symbol_name(b->name)); } jl_printf(JL_STDERR, "WARNING: redefining constant %s\n", jl_symbol_name(b->name)); } } b->value = rhs; jl_gc_wb_binding(b, rhs); }
jl_value_t *jl_method_def(jl_sym_t *name, jl_value_t **bp, jl_binding_t *bnd, jl_tuple_t *argtypes, jl_function_t *f, jl_tuple_t *t) { jl_value_t *gf; if (bnd) { //jl_declare_constant(bnd); if (bnd->value != NULL && !bnd->constp) { jl_errorf("cannot define function %s; it already has a value", bnd->name->name); } bnd->constp = 1; } if (*bp == NULL) { gf = (jl_value_t*)jl_new_generic_function(name); *bp = gf; } else { gf = *bp; if (!jl_is_gf(gf)) { if (jl_is_struct_type(gf) && ((jl_function_t*)gf)->fptr == jl_f_ctor_trampoline) { jl_add_constructors((jl_struct_type_t*)gf); } if (!jl_is_gf(gf)) { jl_error("invalid method definition: not a generic function"); } } } JL_GC_PUSH(&gf); assert(jl_is_function(f)); assert(jl_is_tuple(argtypes)); assert(jl_is_tuple(t)); jl_check_type_tuple(argtypes, name, "method definition"); for(size_t i=0; i < t->length; i++) { if (!jl_is_typevar(jl_tupleref(t,i))) jl_type_error_rt(name->name, "method definition", (jl_value_t*)jl_tvar_type, jl_tupleref(t,i)); } jl_add_method((jl_function_t*)gf, argtypes, f, t); if (jl_boot_file_loaded && f->linfo && f->linfo->ast && jl_is_expr(f->linfo->ast)) { jl_lambda_info_t *li = f->linfo; li->ast = jl_compress_ast(li, li->ast); } JL_GC_POP(); return gf; }