JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef* exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { JSStringRef path_str_ref = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str_ref) < 100); char tmp[100]; tmp[0] = '\0'; JSStringGetUTF8CString(path_str_ref, tmp, 100); JSStringRelease(path_str_ref); char *path = tmp; if (str_has_prefix(path, "goog/../") == 0) { path = path + 8; } char *source = NULL; if (out_path == NULL) { source = bundle_get_contents(path); } else { char *full_path = str_concat(out_path, path); source = get_contents(full_path, NULL); free(full_path); } if (source != NULL) { evaluate_script(ctx, source, path); free(source); } } return JSValueMakeUndefined(ctx); }
char *get_cljs_version() { char *bundle_js = bundle_get_contents("planck/bundle.js"); if (bundle_js != NULL) { char *start = bundle_js + 29; char *version = strtok(start, " "); version = strdup(version); free(bundle_js); return version; } else { return "(Unknown)"; } }
JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { JSStringRef path_str_ref = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str_ref) < 100); char tmp[100]; tmp[0] = '\0'; JSStringGetUTF8CString(path_str_ref, tmp, 100); JSStringRelease(path_str_ref); bool can_skip_load = false; char *path = tmp; if (str_has_prefix(path, "goog/../") == 0) { path = path + 8; } else { unsigned long h = hash((unsigned char *) path); if (is_loaded(h)) { can_skip_load = true; } else { add_loaded_hash(h); } } if (!can_skip_load) { char *source = NULL; if (config.out_path == NULL) { source = bundle_get_contents(path); } else { char *full_path = str_concat(config.out_path, path); source = get_contents(full_path, NULL); free(full_path); } if (source != NULL) { evaluate_script(ctx, source, path); display_launch_timing(path); free(source); } } } return JSValueMakeUndefined(ctx); }
JSValueRef function_load(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { // TODO: implement fully if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char path[PATH_MAX]; JSStringRef path_str = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str) < PATH_MAX); JSStringGetUTF8CString(path_str, path, PATH_MAX); JSStringRelease(path_str); // debug_print_value("load", ctx, args[0]); time_t last_modified = 0; char *contents = NULL; char *loaded_path = strdup(path); bool developing = (config.num_src_paths == 1 && strcmp(config.src_paths[0].type, "src") == 0 && str_has_suffix(config.src_paths[0].path, "/planck-cljs/src/") == 0); if (!developing) { contents = bundle_get_contents(path); last_modified = 0; } // load from classpath if (contents == NULL) { for (int i = 0; i < config.num_src_paths; i++) { if (config.src_paths[i].blacklisted) { continue; } char *type = config.src_paths[i].type; char *location = config.src_paths[i].path; if (strcmp(type, "src") == 0) { char *full_path = str_concat(location, path); contents = get_contents(full_path, &last_modified); if (contents != NULL) { free(loaded_path); loaded_path = strdup(full_path); } free(full_path); } else if (strcmp(type, "jar") == 0) { struct stat file_stat; if (stat(location, &file_stat) == 0) { contents = get_contents_zip(location, path, &last_modified); } else { cljs_perror(location); config.src_paths[i].blacklisted = true; } } if (contents != NULL) { break; } } } // load from out/ if (contents == NULL) { if (config.out_path != NULL) { char *full_path = str_concat(config.out_path, path); contents = get_contents(full_path, &last_modified); free(full_path); } } if (developing && contents == NULL) { contents = bundle_get_contents(path); last_modified = 0; } if (contents != NULL) { JSStringRef contents_str = JSStringCreateWithUTF8CString(contents); free(contents); JSStringRef loaded_path_str = JSStringCreateWithUTF8CString(loaded_path); free(loaded_path); JSValueRef res[3]; res[0] = JSValueMakeString(ctx, contents_str); res[1] = JSValueMakeNumber(ctx, last_modified); res[2] = JSValueMakeString(ctx, loaded_path_str); return JSObjectMakeArray(ctx, 3, res, NULL); } } return JSValueMakeNull(ctx); }