void test_types() { Hemp hemp = hemp_new(); HempContext context = hemp_context(hemp); HempValue number = hemp_num_val(3.14159); ok( hemp_is_number(number), "created a number" ); HempMemory method; method = hemp_object_method(number, "name"); ok( method, "got a pointer to name method"); HempValue value = hemp_send(number, "name", context); ok( hemp_is_defined(value), "got result" ); is( hemp_val_str(value), "Number", "got number name" ); value = hemp_send(number, "no_such_method", context); ok( hemp_is_missing(value), "no_such_method is missing" ); HempText text = hemp_text_from_string("hello world!"); value = hemp_send( hemp_text_val(text), "length", context); ok( hemp_is_defined(value), "got text length" ); eq( hemp_val_int(value), 12, "length is 12" ); hemp_text_free(text); /* try converting number to text */ value = hemp_send(value, "text", context); if (hemp_is_missing(value)) { fail("No text method?"); exit(1); } ok( hemp_is_defined(value), "got length as text" ); is( hemp_val_text(value)->string, "12", "length text is 12" ); // Text value is now managed by context // hemp_text_free( hemp_val_text(value) ); hemp_context_free(context); hemp_free(hemp); }
HempModule hemp_use_module( Hemp hemp, HempString type, HempString name ) { hemp_debug_call("hemp_use_module(%s => %s)\n", type, name); HempValue path = hemp_config_get(hemp, HEMP_CONFIG_MODPATH); HempString string; if (hemp_is_defined(path)) { string = hemp_to_string(path, hemp->context); // hemp_debug_msg("already got %s: %s\n", HEMP_CONFIG_MODPATH, string); } else { HempValue dir = hemp_config_get(hemp, HEMP_CONFIG_DIR); HempValue mod = hemp_config_get(hemp, HEMP_CONFIG_MODDIR); HempString dstr = hemp_to_string(dir, hemp->context); HempString mstr = hemp_to_string(mod, hemp->context); string = hemp_uri_path_join(dstr, mstr, 1); // hemp_debug_msg("constructed %s: %s\n", HEMP_CONFIG_MODPATH, string); /* ugly work-around so we can get the context to manage memory */ HempText text = hemp_context_tmp_text_size(hemp->context, strlen(string) + 1); hemp_text_append_string(text, string); hemp_mem_free(string); string = text->string; // hemp_config_set(hemp, HEMP_CONFIG_MODPATH, hemp_text_val(text)); } // TODO: need a way to save dotted path (hemp.module_path) back into config /* quick hack to get something working */ HempString modpath = getenv("HEMP_MODULE_PATH"); if (! modpath || ! *modpath) { modpath = string; // hemp_debug_msg("No HEMP_MODULE_PATH environment variable set\n"); // return HEMP_FALSE; } /* TODO: sort this mess out */ HempString tpath = hemp_uri_path_join(modpath, type, 1); HempString mpath = hemp_uri_path_join(tpath, name, 1); HempText mtext = hemp_text_from_string(mpath); hemp_text_append_string(mtext, HEMP_MODULE_EXT); hemp_mem_free(mpath); hemp_mem_free(tpath); // hemp_debug_msg("constructed module path: %s\n", mtext->string); HempModule module = hemp_global_module(hemp->global, mtext->string); if (module->binder) { module->binder(module, hemp); } else if (module->error) { hemp_fatal(module->error); } else { /* should never happen - famous last word */ hemp_fatal("No binder function for %s module", name); } hemp_text_free(mtext); return module; }