#include "cell.h" #include "cJSON.h" JSC_CCALL(os_mem_limit, JS_SetMemoryLimit(JS_GetRuntime(js), js2number(js,argv[0]))) JSC_CCALL(os_max_stacksize, JS_SetMaxStackSize(JS_GetRuntime(js), js2number(js,argv[0]))) // Compute the approximate size of a single JS value in memory. JSC_CCALL(os_calc_mem, JSMemoryUsage mu; JS_ComputeMemoryUsage(JS_GetRuntime(js),&mu); ret = JS_NewObject(js); JS_SetPropertyStr(js,ret,"malloc_size",number2js(js,mu.malloc_size)); JS_SetPropertyStr(js,ret,"malloc_limit",number2js(js,mu.malloc_limit)); JS_SetPropertyStr(js,ret,"memory_used_size",number2js(js,mu.memory_used_size)); JS_SetPropertyStr(js,ret,"malloc_count",number2js(js,mu.malloc_count)); JS_SetPropertyStr(js,ret,"memory_used_count",number2js(js,mu.memory_used_count)); JS_SetPropertyStr(js,ret,"str_count",number2js(js,mu.str_count)); JS_SetPropertyStr(js,ret,"str_size",number2js(js,mu.str_size)); JS_SetPropertyStr(js,ret,"obj_count",number2js(js,mu.obj_count)); JS_SetPropertyStr(js,ret,"obj_size",number2js(js,mu.obj_size)); JS_SetPropertyStr(js,ret,"prop_count",number2js(js,mu.prop_count)); JS_SetPropertyStr(js,ret,"prop_size",number2js(js,mu.prop_size)); JS_SetPropertyStr(js,ret,"shape_count",number2js(js,mu.shape_count)); JS_SetPropertyStr(js,ret,"shape_size",number2js(js,mu.shape_size)); JS_SetPropertyStr(js,ret,"js_func_count",number2js(js,mu.js_func_count)); JS_SetPropertyStr(js,ret,"js_func_size",number2js(js,mu.js_func_size)); JS_SetPropertyStr(js,ret,"js_func_code_size",number2js(js,mu.js_func_code_size)); JS_SetPropertyStr(js,ret,"js_func_pc2line_count",number2js(js,mu.js_func_pc2line_count)); JS_SetPropertyStr(js,ret,"js_func_pc2line_size",number2js(js,mu.js_func_pc2line_size)); JS_SetPropertyStr(js,ret,"c_func_count",number2js(js,mu.c_func_count)); JS_SetPropertyStr(js,ret,"array_count",number2js(js,mu.array_count)); JS_SetPropertyStr(js,ret,"fast_array_count",number2js(js,mu.fast_array_count)); JS_SetPropertyStr(js,ret,"fast_array_elements",number2js(js,mu.fast_array_elements)); JS_SetPropertyStr(js,ret,"binary_object_count",number2js(js,mu.binary_object_count)); JS_SetPropertyStr(js,ret,"binary_object_size",number2js(js,mu.binary_object_size)); ) // Evaluate a string via MACH VM JSC_SSCALL(os_eval, if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script."); if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script."); cJSON *ast = JS_ASTTree(str2, strlen(str2), str); if (!ast) return JS_ThrowSyntaxError(js, "eval: failed to parse"); ret = JS_RunMachTree(js, ast, JS_NULL); cJSON_Delete(ast); ) // Disassemble a function object into a string. JSC_CCALL(js_disassemble, return js_debugger_fn_bytecode(js, argv[0]); ) // Return metadata about a given function. JSC_CCALL(js_fn_info, return js_debugger_fn_info(js, argv[0]); ) static const JSCFunctionListEntry js_js_funcs[] = { MIST_FUNC_DEF(os, calc_mem, 0), MIST_FUNC_DEF(os, mem_limit, 1), MIST_FUNC_DEF(os, max_stacksize, 1), MIST_FUNC_DEF(os, eval, 2), MIST_FUNC_DEF(js, disassemble, 1), MIST_FUNC_DEF(js, fn_info, 1), }; JSValue js_js_use(JSContext *js) { JSValue mod = JS_NewObject(js); JS_SetPropertyFunctionList(js,mod,js_js_funcs,countof(js_js_funcs)); return mod; }