void after_each_test() { if (gTempValue != NULL) set_null(gTempValue); caWorld* world = global_world(); world_clear_file_sources(world); }
void source_file_location() { test_write_fake_file("block.ca", 1, "a = 1"); Block* block = load_module_file(global_world(), temp_string("source_file_location"), "block.ca"); test_equals(block_get_source_filename(block), "block.ca"); }
void type_name_visible_from_module() { FakeFilesystem fs; fs.set("a", "type A { int i }"); load_module_file(global_world(), "a", "a"); fs.set("b", "require a\ntest_spy(make(A))"); Block* b = load_module_file(global_world(), "b", "b"); Stack stack; push_frame(&stack, b); test_spy_clear(); run_interpreter(&stack); test_assert(&stack); test_equals(test_spy_get_results(), "[{i: 0}]"); }
void before_each_test() { if (gFakeFileMap == NULL) gFakeFileMap = new Value(); set_mutable_hashtable(gFakeFileMap); caWorld* world = global_world(); world_clear_file_sources(world); set_value(world_append_file_source(world), gFakeFileMap); }
Block::Block(World* _world) : owningTerm(NULL), inProgress(false), world(_world) { if (_world == NULL) world = global_world(); id = world->nextBlockID++; on_block_created(this); }
void non_required_module_is_not_visible() { World* world = global_world(); test_write_fake_file("module_a.ca", 1, "a = 1"); test_write_fake_file("module_b.ca", 1, "b = 1"); Block* module_a = load_module_file(world, temp_string("module_a"), "module_a.ca"); Block* module_b = load_module_file(world, temp_string("module_b"), "module_b.ca"); test_assert(find_name(module_a, "a") != NULL); test_assert(find_name(module_b, "a") == NULL); }
void global_names() { Block* block = find_or_create_block(global_root_block(), "names_test"); Term* a = block->compile("a = 1"); circa::Value globalName; get_global_name(a, &globalName); test_equals(&globalName, "names_test:a"); Term* a_2 = block->compile("a = 2"); get_global_name(a, &globalName); test_equals(&globalName, "names_test:a#1"); get_global_name(a_2, &globalName); test_equals(&globalName, "names_test:a#2"); Block* block2 = create_block(block, "block2"); Term* b = block2->compile("b = 3"); Term* b_2 = block2->compile("b = 4"); get_global_name(b, &globalName); test_equals(&globalName, "names_test:block2:b#1"); get_global_name(b_2, &globalName); test_equals(&globalName, "names_test:block2:b#2"); // Now try finding terms via their global name. test_assert(find_from_global_name(global_world(), "names_test:a") == a_2); test_assert(find_from_global_name(global_world(), "names_test:a#1") == a); test_assert(find_from_global_name(global_world(), "names_test:a#2") == a_2); test_assert(find_from_global_name(global_world(), "names_test:block2:b") == b_2); test_assert(find_from_global_name(global_world(), "names_test:block2:b#1") == b); test_assert(find_from_global_name(global_world(), "names_test:block2:b#2") == b_2); }
void test_explicit_output() { World* world = global_world(); test_write_fake_file("module.ca", 1, "99 -> output"); circa_load_module_from_file(world, "Module", "module.ca"); Stack* stack = circa_create_stack(world); circa_push_module(stack, "Module"); circa_run(stack); test_assert(stack); test_equals(circa_output(stack, 0), "99"); circa_free_stack(stack); }
CIRCA_EXPORT caWorld* circa_initialize() { bootstrap_kernel(); caWorld* world = global_world(); Block* builtins = global_builtins_block(); //dump(builtins); // Make sure there are no static errors in builtins. This shouldn't happen. #if 0 if (has_static_errors(builtins)) { std::cout << "Static errors found in kernel:" << std::endl; dump(builtins); Value msg; print_static_errors_formatted(builtins, &msg); dump(&msg); } #endif // Load library paths from CIRCA_LIB_PATH const char* libPathEnv = getenv("CIRCA_LIB_PATH"); if (libPathEnv != NULL) { Value libPathStr; set_string(&libPathStr, libPathEnv); Value libPaths; string_split(&libPathStr, ';', &libPaths); for (int i=0; i < list_length(&libPaths); i++) { Value* path = list_get(&libPaths, i); if (string_equals(path, "")) continue; module_add_search_path(world, as_cstring(path)); } } log_msg(0, "finished circa_initialize"); world->bootstrapStatus = s_Done; return world; }
CIRCA_EXPORT caWorld* circa_initialize() { memset(&FUNCS, 0, sizeof(FUNCS)); memset(&TYPES, 0, sizeof(TYPES)); bootstrap_kernel(); caWorld* world = global_world(); Block* kernel = global_root_block(); // Make sure there are no static errors in the kernel. This shouldn't happen. if (has_static_errors(kernel)) { std::cout << "Static errors found in kernel:" << std::endl; print_static_errors_formatted(kernel, std::cout); internal_error("circa fatal: static errors found in kernel"); } // Load library paths from CIRCA_LIB_PATH const char* libPathEnv = getenv("CIRCA_LIB_PATH"); if (libPathEnv != NULL) { Value libPathStr; set_string(&libPathStr, libPathEnv); Value libPaths; string_split(&libPathStr, ';', &libPaths); for (int i=0; i < list_length(&libPaths); i++) { caValue* path = list_get(&libPaths, i); if (string_eq(path, "")) continue; module_add_search_path(world, as_cstring(path)); } } log_msg(0, "finished circa_initialize"); world->bootstrapStatus = sym_Done; return world; }
void search_every_global_name() { // This test is brave. We go through every single term in the world, find its // global name (if it exists), then see if we can find the original term using // the global name. circa::Value globalName; for (BlockIterator it(global_root_block()); it.unfinished(); it.advance()) { get_global_name(*it, &globalName); if (!is_string(&globalName)) continue; Term* searchResult = find_from_global_name(global_world(), as_cstring(&globalName)); if (searchResult != *it) { std::cout << "Global name search failed for term: " << global_id(*it) << ", with global name: " << as_cstring(&globalName) << std::endl; declare_current_test_failed(); } } }
Block* global_builtins_block() { return global_world()->builtins; }
Block* find_builtins_block(Block* block) { Block* builtins = global_world()->builtins; return builtins; }
Block* global_root_block() { return global_world()->root; }
void on_new_function_parsed(Term* func, Value* functionName) { if (global_world()->bootstrapStatus == s_Done) return; #define find_func(name, sourceName) if (string_equals(functionName, sourceName)) FUNCS.name = func; find_func(add_i, "add_i"); find_func(add_f, "add_f"); find_func(annotate, "annotate"); find_func(annotate_block, "annotate_block"); find_func(and_func, "and"); find_func(break_func, "break"); find_func(blank_list, "blank_list"); find_func(case_func, "case"); find_func(cast, "cast"); find_func(cond, "cond"); find_func(continue_func, "continue"); find_func(copy, "copy"); find_func(declare_field, "declare_field"); find_func(discard, "discard"); find_func(div_f, "div_f"); find_func(div_i, "div_i"); find_func(dynamic_method, "dynamic_method"); find_func(error, "error"); find_func(equals, "equals"); find_func(extra_output, "extra_output"); find_func(for_func, "for"); find_func(func_call_implicit, "func_call_implicit"); find_func(function_decl, "function_decl"); find_func(get_field, "get_field"); find_func(get_index, "get_index"); find_func(get_with_selector, "get_with_selector"); find_func(greater_than, "greater_than"); find_func(greater_than_eq, "greater_than_eq"); find_func(if_block, "if"); find_func(inputs_fit_function, "inputs_fit_function"); find_func(length, "length"); find_func(less_than, "less_than"); find_func(less_than_eq, "less_than_eq"); find_func(loop_condition_bool, "loop_condition_bool"); find_func(loop_iterator, "loop_iterator"); find_func(make_table, "make_table"); find_func(make, "make"); find_func(make_list, "make_list"); find_func(method_lookup, "method_lookup"); find_func(native_patch, "native_patch"); find_func(neg, "neg"); find_func(not_equals, "not_equals"); find_func(not_func, "not"); find_func(or_func, "or"); find_func(overload_error_no_match, "overload_error_no_match"); find_func(range, "range"); find_func(return_func, "return"); find_func(remainder, "remainder"); find_func(require, "require"); find_func(require_check, "require_check"); find_func(require_local, "require_local"); find_func(section_block, "section"); find_func(selector, "selector"); find_func(set_index, "set_index"); find_func(set_field, "set_field"); find_func(set_with_selector, "set_with_selector"); find_func(static_error, "static_error"); find_func(sub_i, "sub_i"); find_func(sub_f, "sub_f"); find_func(switch_func, "switch"); find_func(syntax_error, "syntax_error"); find_func(to_seq, "to_seq"); find_func(type, "type"); find_func(unknown_function, "unknown_function"); find_func(unknown_function_prelude, "unknown_function_prelude"); find_func(unknown_identifier, "unknown_identifier"); find_func(while_loop, "while"); find_func(add, "add"); find_func(div, "div"); find_func(mult, "mult"); find_func(sub, "sub"); find_func(closure_block, "closure_block"); find_func(declared_state, "declared_state"); find_func(declared_state_close, "declared_state_close"); find_func(dynamic_method, "dynamic_method"); find_func(dynamic_term_eval, "_dynamic_term_eval"); find_func(equals, "equals"); find_func(error, "error"); find_func(eval_on_demand, "_eval_on_demand"); find_func(func_call, "call"); find_func(func_call_method, "Func.call"); find_func(func_apply, "func_apply"); find_func(func_apply_method, "Func.apply"); find_func(get_with_selector, "get_with_selector"); find_func(table_get, "Table.get"); find_func(table_get_and_call, "Table.get_and_call"); find_func(module_get, "Module._get"); find_func(not_equals, "not_equals"); find_func(selector, "selector"); find_func(set_with_selector, "set_with_selector"); find_func(type_make, "Type.make"); find_func(upvalue, "upvalue"); find_func(vm_save_declared_state, "vm_save_declared_state"); find_func(vm_close_stateful_minor_frame, "vm_close_stateful_minor_frame"); #undef find_func #define has_custom_type_infer(name) \ if (FUNCS.name != NULL) \ block_set_specialize_type_func(nested_contents(FUNCS.name), name##_specializeType) has_custom_type_infer(cond); has_custom_type_infer(copy); has_custom_type_infer(extra_output); has_custom_type_infer(get_field); has_custom_type_infer(get_index); has_custom_type_infer(make); has_custom_type_infer(range); has_custom_type_infer(set_field); has_custom_type_infer(set_index); has_custom_type_infer(type_make); #undef has_custom_type_infer #define has_post_compile(sourceName, f) if (string_equals(functionName, sourceName)) \ block_set_post_compile_func(nested_contents(func), f); has_post_compile("return", controlFlow_postCompile); has_post_compile("discard", controlFlow_postCompile); has_post_compile("break", controlFlow_postCompile); has_post_compile("continue", controlFlow_postCompile); #undef has_post_compile }
void write_log(const char* msg) { write_log(global_world(), msg); }
World* test_world() { return global_world(); }