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->owner == m || mb->imported) { m = (jl_module_t*)mb->value; if (m == NULL || !jl_is_module(m)) jl_errorf("invalid module path"); 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; }
static jl_module_t *eval_import_path(jl_array_t *args) { // 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) jl_errorf("in module path: %s not defined", var->name); m = m->parent; } for(size_t i=1; 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_check_static_parameter_conflicts(jl_lambda_info_t *li, jl_svec_t *t, jl_sym_t *fname) { jl_array_t *vinfo; size_t nvars; if (li->ast && jl_is_expr(li->ast)) { vinfo = jl_lam_vinfo((jl_expr_t*)li->ast); nvars = jl_array_len(vinfo); for(size_t i=0; i < jl_svec_len(t); i++) { for(size_t j=0; j < nvars; j++) { jl_value_t *tv = jl_svecref(t,i); if (jl_is_typevar(tv)) { if ((jl_sym_t*)jl_cellref((jl_array_t*)jl_cellref(vinfo,j),0) == ((jl_tvar_t*)tv)->name) { jl_printf(JL_STDERR, "WARNING: local variable %s conflicts with a static parameter in %s", ((jl_tvar_t*)tv)->name->name, fname->name); print_func_loc(JL_STDERR, li); jl_printf(JL_STDERR, ".\n"); } } } } } }
static jl_function_t *jl_method_table_assoc_exact(jl_methtable_t *mt, jl_value_t **args, size_t n) { jl_methlist_t *ml = NULL; if (n > 0) { jl_value_t *a0 = args[0]; jl_value_t *ty = (jl_value_t*)jl_typeof(a0); uptrint_t uid; if ((ty == (jl_value_t*)jl_struct_kind && (uid = ((jl_struct_type_t*)a0)->uid)) || (ty == (jl_value_t*)jl_bits_kind && (uid = ((jl_bits_type_t*)a0)->uid))) { if (mt->cache_targ && uid < jl_array_len(mt->cache_targ)) { ml = (jl_methlist_t*)jl_cellref(mt->cache_targ, uid); if (ml) goto mt_assoc_lkup; } } if ((jl_is_struct_type(ty) && (uid = ((jl_struct_type_t*)ty)->uid)) || (jl_is_bits_type(ty) && (uid = ((jl_bits_type_t*)ty)->uid))) { if (mt->cache_arg1 && uid < jl_array_len(mt->cache_arg1)) { ml = (jl_methlist_t*)jl_cellref(mt->cache_arg1, uid); if (ml) { if (ml->next==NULL && n==1 && ml->sig->length==1) return ml->func; if (n==2) { // some manually-unrolled common special cases jl_value_t *a1 = args[1]; jl_methlist_t *mn = ml; if (mn->sig->length==2 && jl_tupleref(mn->sig,1)==(jl_value_t*)jl_typeof(a1)) return mn->func; mn = mn->next; if (mn && mn->sig->length==2 && jl_tupleref(mn->sig,1)==(jl_value_t*)jl_typeof(a1)) return mn->func; } } } } } if (ml == NULL) ml = mt->cache; mt_assoc_lkup: while (ml != NULL) { if (((jl_tuple_t*)ml->sig)->length == n || ml->va==jl_true) { if (cache_match(args, n, (jl_tuple_t*)ml->sig, ml->va==jl_true)) { return ml->func; } } ml = ml->next; } return NULL; }
// TODO: eval decl types for arguments of non-generic functions static void eval_decl_types(jl_array_t *vi, jl_tuple_t *spenv) { size_t i; for(i=0; i < jl_array_len(vi); i++) { jl_array_t *v = (jl_array_t*)jl_cellref(vi, i); assert(jl_array_len(v) > 1); jl_value_t *ty = jl_interpret_toplevel_expr_with(jl_cellref(v,1), &jl_tupleref(spenv,0), jl_tuple_len(spenv)/2); jl_cellref(v, 1) = ty; } }
static inline union jl_typemap_t mtcache_hash_lookup(jl_array_t *a, jl_value_t *ty, int8_t tparam, int8_t offs) { uintptr_t uid = ((jl_datatype_t*)ty)->uid; union jl_typemap_t ml; ml.unknown = jl_nothing; if (!uid) return ml; ml.unknown = jl_cellref(a, uid & (a->nrows-1)); if (ml.unknown != NULL && ml.unknown != jl_nothing) { jl_value_t *t; if (jl_typeof(ml.unknown) == (jl_value_t*)jl_typemap_level_type) { t = ml.node->key; } else { t = jl_field_type(ml.leaf->sig, offs); if (tparam) t = jl_tparam0(t); } if (t == ty) return ml; } ml.unknown = jl_nothing; return ml; }
// heuristic for whether a top-level input should be evaluated with // the compiler or the interpreter. static int eval_with_compiler_p(jl_expr_t *expr, int compileloops) { if (expr->head==body_sym) { jl_array_t *body = expr->args; size_t i; for(i=0; i < body->length; i++) { jl_value_t *stmt = jl_cellref(body,i); if (jl_is_expr(stmt)) { // TODO: only backward branches if (compileloops && (((jl_expr_t*)stmt)->head == goto_sym || ((jl_expr_t*)stmt)->head == goto_ifnot_sym)) { return 1; } // to compile code that uses exceptions /* if (((jl_expr_t*)stmt)->head == enter_sym) { return 1; } */ } } } if (has_intrinsics(expr)) return 1; return 0; }
// load toplevel expressions, from (file ...) void jl_load_file_expr(char *fname, jl_value_t *ast) { jl_array_t *b = ((jl_expr_t*)ast)->args; size_t i; volatile size_t lineno=0; if (((jl_expr_t*)ast)->head == jl_continue_sym) { jl_errorf("syntax error: %s", jl_string_data(jl_exprarg(ast,0))); } JL_TRY { jl_register_toplevel_eh(); // handle syntax error if (((jl_expr_t*)ast)->head == error_sym) { jl_interpret_toplevel_expr(ast); } for(i=0; i < b->length; i++) { // process toplevel form jl_value_t *form = jl_cellref(b, i); if (jl_is_linenode(form)) { lineno = jl_linenode_line(form); } else { (void)jl_toplevel_eval_flex(form, 0, &lineno); } } } JL_CATCH { jl_value_t *fn=NULL, *ln=NULL; JL_GC_PUSH(&fn, &ln); fn = jl_pchar_to_string(fname, strlen(fname)); ln = jl_box_long(lineno); jl_raise(jl_new_struct(jl_loaderror_type, fn, ln, jl_exception_in_transit)); } }
// TODO: eval decl types for arguments of non-generic functions static void eval_decl_types(jl_array_t *vi, jl_value_t *ast, jl_tuple_t *spenv) { size_t i, l = jl_array_len(vi); for(i=0; i < l; i++) { jl_array_t *v = (jl_array_t*)jl_cellref(vi, i); assert(jl_array_len(v) > 1); jl_value_t *ty = jl_static_eval(jl_cellref(v,1), NULL, jl_current_module, (jl_value_t*)spenv, (jl_expr_t*)ast, 1, 1); if (ty != NULL && (jl_is_type(ty) || jl_is_typevar(ty))) { jl_cellref(v, 1) = ty; } else { jl_cellref(v, 1) = (jl_value_t*)jl_any_type; } } }
// heuristic for whether a top-level input should be evaluated with // the compiler or the interpreter. int jl_eval_with_compiler_p(jl_expr_t *expr, int compileloops) { assert(jl_is_expr(expr)); if (expr->head==body_sym && compileloops) { jl_array_t *body = expr->args; size_t i, maxlabl=0; // compile if there are backwards branches for(i=0; i < body->length; i++) { jl_value_t *stmt = jl_cellref(body,i); if (jl_is_labelnode(stmt)) { int l = jl_labelnode_label(stmt); if (l > maxlabl) maxlabl = l; } } size_t sz = (maxlabl+1+7)/8; char *labls = alloca(sz); memset(labls,0,sz); for(i=0; i < body->length; i++) { jl_value_t *stmt = jl_cellref(body,i); if (jl_is_labelnode(stmt)) { int l = jl_labelnode_label(stmt); labls[l/8] |= (1<<(l&7)); } else if (compileloops && jl_is_gotonode(stmt)) { int l = jl_gotonode_label(stmt); if (labls[l/8]&(1<<(l&7))) { return 1; } } else if (jl_is_expr(stmt)) { if (compileloops && ((jl_expr_t*)stmt)->head==goto_ifnot_sym) { int l = jl_unbox_long(jl_exprarg(stmt,1)); if (labls[l/8]&(1<<(l&7))) { return 1; } } // to compile code that uses exceptions /* if (((jl_expr_t*)stmt)->head == enter_sym) { return 1; } */ } } } if (has_intrinsics(expr)) return 1; return 0; }
// get array of var info records jl_array_t *jl_lam_vinfo(jl_expr_t *l) { jl_value_t *le = jl_exprarg(l, 1); assert(jl_is_array(le)); jl_value_t *ll = jl_cellref(le, 1); assert(jl_is_array(ll)); return (jl_array_t*)ll; }
static jl_value_t *copy_ast(jl_value_t *expr, jl_tuple_t *sp, int do_sp) { if (jl_is_symbol(expr)) { if (!do_sp) return expr; // pre-evaluate certain static parameters to help type inference for(int i=0; i < jl_tuple_len(sp); i+=2) { assert(jl_is_typevar(jl_tupleref(sp,i))); if ((jl_sym_t*)expr == ((jl_tvar_t*)jl_tupleref(sp,i))->name) { jl_value_t *spval = jl_tupleref(sp,i+1); if (jl_is_long(spval)) return spval; } } } else if (jl_is_lambda_info(expr)) { jl_lambda_info_t *li = (jl_lambda_info_t*)expr; /* if (sp == jl_null && li->ast && jl_lam_capt((jl_expr_t*)li->ast)->length == 0) return expr; */ // TODO: avoid if above condition is true and decls have already // been evaluated. JL_GC_PUSH(&li); li = jl_add_static_parameters(li, sp); li->ast = jl_prepare_ast(li, li->sparams); JL_GC_POP(); return (jl_value_t*)li; } else if (jl_typeis(expr,jl_array_any_type)) { jl_array_t *a = (jl_array_t*)expr; jl_array_t *na = jl_alloc_cell_1d(jl_array_len(a)); JL_GC_PUSH(&na); size_t i; for(i=0; i < jl_array_len(a); i++) jl_cellset(na, i, copy_ast(jl_cellref(a,i), sp, do_sp)); JL_GC_POP(); return (jl_value_t*)na; } else if (jl_is_expr(expr)) { jl_expr_t *e = (jl_expr_t*)expr; jl_expr_t *ne = jl_exprn(e->head, jl_array_len(e->args)); JL_GC_PUSH(&ne); size_t i; if (e->head == lambda_sym) { jl_exprarg(ne, 0) = copy_ast(jl_exprarg(e,0), sp, 0); jl_exprarg(ne, 1) = copy_ast(jl_exprarg(e,1), sp, 0); jl_exprarg(ne, 2) = copy_ast(jl_exprarg(e,2), sp, 1); } else { for(i=0; i < jl_array_len(e->args); i++) jl_exprarg(ne, i) = copy_ast(jl_exprarg(e,i), sp, 1); } JL_GC_POP(); return (jl_value_t*)ne; } return expr; }
// get array of var info records for captured vars jl_array_t *jl_lam_capt(jl_expr_t *l) { assert(jl_is_expr(l)); jl_value_t *le = jl_exprarg(l, 1); assert(jl_is_array(le)); jl_value_t *ll = jl_cellref(le, 2); assert(jl_is_array(ll)); return (jl_array_t*)ll; }
static int literal_val_id(jl_value_t *v) { for(int i=0; i < jl_array_len(tree_literal_values); i++) { if (jl_cellref(tree_literal_values,i) == v) return i; } jl_cell_1d_push(tree_literal_values, v); return jl_array_len(tree_literal_values)-1; }
static jl_function_t *jl_method_cache_insert(jl_methtable_t *mt, jl_tuple_t *type, jl_function_t *method) { jl_methlist_t **pml = &mt->cache; if (type->length > 0) { jl_value_t *t0 = jl_t0(type); uptrint_t uid=0; // if t0 != jl_typetype_type and the argument is Type{...}, this // method has specializations for singleton kinds and we use // the table indexed for that purpose. if (t0 != (jl_value_t*)jl_typetype_type && jl_is_type_type(t0)) { jl_value_t *a0 = jl_tparam0(t0); if (jl_is_struct_type(a0)) uid = ((jl_struct_type_t*)a0)->uid; else if (jl_is_bits_type(a0)) uid = ((jl_bits_type_t*)a0)->uid; if (uid > 0) { if (mt->cache_targ == NULL) mt->cache_targ = jl_alloc_cell_1d(0); if (uid >= jl_array_len(mt->cache_targ)) { jl_array_grow_end(mt->cache_targ, uid+4-jl_array_len(mt->cache_targ)); } pml = (jl_methlist_t**)&jl_cellref(mt->cache_targ, uid); goto ml_do_insert; } } if (jl_is_struct_type(t0)) uid = ((jl_struct_type_t*)t0)->uid; else if (jl_is_bits_type(t0)) uid = ((jl_bits_type_t*)t0)->uid; if (uid > 0) { if (mt->cache_arg1 == NULL) mt->cache_arg1 = jl_alloc_cell_1d(0); if (uid >= jl_array_len(mt->cache_arg1)) { jl_array_grow_end(mt->cache_arg1, uid+4-jl_array_len(mt->cache_arg1)); } pml = (jl_methlist_t**)&jl_cellref(mt->cache_arg1, uid); } } ml_do_insert: return jl_method_list_insert(pml, type, method, jl_null, 0)->func; }
void jl_typemap_rehash_array(jl_array_t **pa, jl_value_t *parent, int8_t tparam, int8_t offs) { size_t i, len = (*pa)->nrows; for (i = 0; i < len; i++) { union jl_typemap_t ml; ml.unknown = jl_cellref(*pa, i); assert(ml.unknown != NULL); jl_typemap_rehash(ml, offs+1); } mtcache_rehash(pa, parent, tparam, offs); }
void jl_lambda_info_set_ast(jl_lambda_info_t *li, jl_value_t *ast) { assert(jl_is_expr(ast)); jl_array_t *body = jl_lam_body((jl_expr_t*)ast)->args; li->code = body; jl_gc_wb(li, li->code); if (has_meta(body, pure_sym)) li->pure = 1; jl_array_t *vis = jl_lam_vinfo((jl_expr_t*)ast); size_t nslots = jl_array_len(vis); jl_value_t *ssavalue_types = jl_lam_ssavalues((jl_expr_t*)ast); assert(jl_is_long(ssavalue_types)); size_t nssavalue = jl_unbox_long(ssavalue_types); li->slotnames = jl_alloc_cell_1d(nslots); jl_gc_wb(li, li->slotnames); li->slottypes = jl_nothing; li->slotflags = jl_alloc_array_1d(jl_array_uint8_type, nslots); jl_gc_wb(li, li->slotflags); li->ssavaluetypes = jl_box_long(nssavalue); jl_gc_wb(li, li->ssavaluetypes); int i; for(i=0; i < nslots; i++) { jl_value_t *vi = jl_cellref(vis, i); jl_sym_t *name = (jl_sym_t*)jl_cellref(vi, 0); assert(jl_is_symbol(name)); char *str = jl_symbol_name(name); if (i > 0 && name != unused_sym) { if (str[0] == '#') { // convention for renamed variables: #...#original_name char *nxt = strchr(str + 1, '#'); if (nxt) name = jl_symbol(nxt+1); else if (str[1] == 's') // compiler-generated temporaries, #sXXX name = compiler_temp_sym; } } jl_cellset(li->slotnames, i, name); jl_array_uint8_set(li->slotflags, i, jl_unbox_long(jl_cellref(vi, 2))); } jl_array_t *args = jl_lam_args((jl_expr_t*)ast); size_t narg = jl_array_len(args); li->nargs = narg; li->isva = narg > 0 && jl_is_rest_arg(jl_cellref(args, narg - 1)); }
int jl_lam_vars_captured(jl_expr_t *ast) { jl_array_t *vinfos = jl_lam_vinfo(ast); for(int i=0; i < jl_array_len(vinfos); i++) { if (jl_vinfo_capt((jl_array_t*)jl_cellref(vinfos,i))) { return 1; } } return 0; }
static jl_value_t *eval_body(jl_array_t *stmts, jl_value_t **locals, size_t nl, int start) { jl_savestate_t __ss; jl_jmp_buf __handlr; size_t i=start; while (1) { jl_value_t *stmt = jl_cellref(stmts,i); if (jl_is_gotonode(stmt)) { i = label_idx(jl_fieldref(stmt,0), stmts); continue; } if (jl_is_expr(stmt)) { jl_sym_t *head = ((jl_expr_t*)stmt)->head; if (head == goto_ifnot_sym) { jl_value_t *cond = eval(jl_exprarg(stmt,0), locals, nl); if (cond == jl_false) { i = label_idx(jl_exprarg(stmt,1), stmts); continue; } else if (cond != jl_true) { jl_type_error_rt("toplevel", "if", (jl_value_t*)jl_bool_type, cond); } } else if (head == return_sym) { return eval(jl_exprarg(stmt,0), locals, nl); } else if (head == enter_sym) { jl_enter_handler(&__ss, &__handlr); if (!jl_setjmp(__handlr,1)) { return eval_body(stmts, locals, nl, i+1); } else { i = label_idx(jl_exprarg(stmt,0), stmts); continue; } } else if (head == leave_sym) { int hand_n_leave = jl_unbox_long(jl_exprarg(stmt,0)); jl_pop_handler(hand_n_leave); } else { eval(stmt, locals, nl); } } else { eval(stmt, locals, nl); } i++; } assert(0); return NULL; }
//this function is for factor convert it maybe not safe //because PooledDataArray.refs is Uint32 or bigger //but in pratice it should be ok static SEXP Julia_R_MD_INT(jl_value_t *Var) { SEXP ans = R_NilValue; jl_value_t *val; if (((jl_array_t *)Var)->ptrarray) val = jl_cellref(Var, 0); else val = jl_arrayref((jl_array_t *)Var, 0); int len = jl_array_len(Var); if (len == 0) return ans; if (jl_is_int32(val)) { int32_t *p = (int32_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_int64(val)) { int64_t *p = (int64_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_int8(val)) { int8_t *p = (int8_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_int16(val)) { int16_t *p = (int16_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_uint8(val)) { uint8_t *p = (uint8_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_uint16(val)) { uint16_t *p = (uint16_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_uint32(val)) { uint32_t *p = (uint32_t *) jl_array_data(Var); jlint_to_r_md; } else if (jl_is_uint64(val)) { uint64_t *p = (uint64_t *) jl_array_data(Var); jlint_to_r_md; } return ans; }
static void array_to_list(jl_array_t *a, value_t *pv) { if (jl_array_len(a) > 300000) lerror(MemoryError, "expression too large"); value_t temp; for(long i=jl_array_len(a)-1; i >= 0; i--) { *pv = fl_cons(FL_NIL, *pv); temp = julia_to_scm_(jl_cellref(a,i)); // note: must be separate statement car_(*pv) = temp; } }
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; }
static int label_idx(jl_value_t *tgt, jl_array_t *stmts) { size_t j; long ltgt = jl_unbox_long(tgt); for(j=0; j < stmts->nrows; j++) { jl_value_t *l = jl_cellref(stmts,j); if (jl_is_labelnode(l) && jl_labelnode_label(l)==ltgt) break; } assert(j < stmts->nrows); return j; }
/* Method caches are divided into three parts: one for signatures where the first argument is a singleton kind (Type{Foo}), one indexed by the UID of the first argument's type in normal cases, and a fallback table of everything else. */ static jl_function_t *jl_method_table_assoc_exact_by_type(jl_methtable_t *mt, jl_tuple_t *types) { jl_methlist_t *ml = NULL; if (types->length > 0) { jl_value_t *ty = jl_t0(types); uptrint_t uid; if (jl_is_type_type(ty)) { jl_value_t *a0 = jl_tparam0(ty); jl_value_t *tty = (jl_value_t*)jl_typeof(a0); if ((tty == (jl_value_t*)jl_struct_kind && (uid = ((jl_struct_type_t*)a0)->uid)) || (tty == (jl_value_t*)jl_bits_kind && (uid = ((jl_bits_type_t*)a0)->uid))) { if (mt->cache_targ && uid < jl_array_len(mt->cache_targ)) { ml = (jl_methlist_t*)jl_cellref(mt->cache_targ, uid); if (ml) goto mt_assoc_bt_lkup; } } } if ((jl_is_struct_type(ty) && (uid = ((jl_struct_type_t*)ty)->uid)) || (jl_is_bits_type(ty) && (uid = ((jl_bits_type_t*)ty)->uid))) { if (mt->cache_arg1 && uid < jl_array_len(mt->cache_arg1)) { ml = (jl_methlist_t*)jl_cellref(mt->cache_arg1, uid); } } } if (ml == NULL) ml = mt->cache; mt_assoc_bt_lkup: while (ml != NULL) { if (cache_match_by_type(&jl_tupleref(types,0), types->length, (jl_tuple_t*)ml->sig, ml->va==jl_true)) { return ml->func; } ml = ml->next; } return NULL; }
// get array of var info records for captured vars jl_array_t *jl_lam_capt(jl_expr_t *l) { if (jl_is_tuple(l)) { // in compressed form return (jl_array_t*)jl_tupleref(l, 3); } assert(jl_is_expr(l)); jl_value_t *le = jl_exprarg(l, 1); assert(jl_is_array(le)); jl_value_t *ll = jl_cellref(le, 2); assert(jl_is_array(ll)); return (jl_array_t*)ll; }
static value_t array_to_list(jl_array_t *a) { long i; value_t lst=FL_NIL, temp=FL_NIL; fl_gc_handle(&lst); fl_gc_handle(&temp); for(i=jl_array_len(a)-1; i >= 0; i--) { temp = julia_to_scm(jl_cellref(a,i)); lst = fl_cons(temp, lst); } fl_free_gc_handles(2); return lst; }
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 value_t array_to_list(jl_array_t *a) { if (jl_array_len(a) > 300000) jl_error("expression too large"); value_t lst=FL_NIL, temp=FL_NIL; fl_gc_handle(&lst); fl_gc_handle(&temp); for(long i=jl_array_len(a)-1; i >= 0; i--) { temp = julia_to_scm(jl_cellref(a,i)); lst = fl_cons(temp, lst); } fl_free_gc_handles(2); return lst; }
// load toplevel expressions, from (file ...) void jl_load_file_expr(char *fname, jl_value_t *ast) { jl_array_t *b = ((jl_expr_t*)ast)->args; size_t i; volatile size_t lineno=0; if (((jl_expr_t*)ast)->head == jl_continue_sym) { jl_errorf("syntax error: %s", jl_string_data(jl_exprarg(ast,0))); } char oldcwd[512]; char newcwd[512]; get_cwd(oldcwd, sizeof(oldcwd)); char *sep = strrchr(fname, PATHSEP); if (sep) { size_t n = (sep - fname)+1; if (n > sizeof(newcwd)-1) n = sizeof(newcwd)-1; strncpy(newcwd, fname, n); newcwd[n] = '\0'; set_cwd(newcwd); } JL_TRY { jl_register_toplevel_eh(); // handle syntax error if (((jl_expr_t*)ast)->head == error_sym) { jl_interpret_toplevel_expr(ast); } for(i=0; i < b->length; i++) { // process toplevel form jl_value_t *form = jl_cellref(b, i); if (jl_is_linenode(form)) { lineno = jl_linenode_line(form); } else { (void)jl_toplevel_eval_flex(form, 0, &lineno); } } } JL_CATCH { if (sep) set_cwd(oldcwd); jl_value_t *fn=NULL, *ln=NULL; JL_GC_PUSH(&fn, &ln); fn = jl_pchar_to_string(fname, strlen(fname)); ln = jl_box_long(lineno); jl_raise(jl_new_struct(jl_loaderror_type, fn, ln, jl_exception_in_transit)); } if (sep) set_cwd(oldcwd); }
jl_value_t *jl_toplevel_eval_flex(jl_value_t *ex, int fast) { //jl_show(ex); //ios_printf(ios_stdout, "\n"); jl_lambda_info_t *thk; int ewc = 0; if (jl_typeof(ex) != (jl_type_t*)jl_lambda_info_type) { if (jl_is_expr(ex) && eval_with_compiler_p((jl_expr_t*)ex, fast)) { thk = jl_wrap_expr(ex); ewc = 1; } else { return jl_interpret_toplevel_expr(ex); } } else { thk = (jl_lambda_info_t*)ex; ewc = eval_with_compiler_p(jl_lam_body((jl_expr_t*)thk->ast), fast); if (!ewc) { jl_array_t *vinfos = jl_lam_vinfo((jl_expr_t*)thk->ast); int i; for(i=0; i < vinfos->length; i++) { if (jl_vinfo_capt((jl_array_t*)jl_cellref(vinfos,i))) { // interpreter doesn't handle closure environment ewc = 1; break; } } } } jl_value_t *thunk=NULL; jl_function_t *gf=NULL; jl_value_t *result; JL_GC_PUSH(&thunk, &gf, &thk); if (ewc) { thunk = jl_new_closure_internal(thk, (jl_value_t*)jl_null); result = jl_apply((jl_function_t*)thunk, NULL, 0); } else { result = jl_interpret_toplevel_thunk(thk); } JL_GC_POP(); return result; }