diff --git a/bindings.cpp b/bindings.cpp index a52a5c2..b0a6827 100644 --- a/bindings.cpp +++ b/bindings.cpp @@ -20,6 +20,13 @@ void cleanup_context(napi_env env, void *finalize_data, void *finalize_hint) duk_destroy_heap(ctx); } +void fatal_handler (void *udata, const char *msg) +{ + HeapConfig *heapData = (HeapConfig *)udata; + napi_env env = (napi_env)heapData->napi_env; + napi_throw_error(env, nullptr, msg ? msg : "Duktape fatal error"); +}; + napi_value create_context(napi_env env, napi_callback_info info) { size_t argc = 1; @@ -45,15 +52,17 @@ napi_value create_context(napi_env env, napi_callback_info info) gasData->gas_limit = gas_limit; gasData->gas_used = 0; gasData->mem_cost_per_byte = mem_cost_per_byte; - auto fatal_handler = [](void *udata, const char *msg) - { - napi_env env = static_cast(udata); - napi_throw_error(env, nullptr, msg ? msg : "Duktape fatal error"); - }; - duk_context *ctx = duk_create_heap(duk_gas_respecting_alloc_function, duk_gas_respecting_realloc_function, duk_gas_respecting_free_function, gasData, (duk_fatal_function)fatal_handler); + + auto *heapConfig = new HeapConfig; + heapConfig->gasConfig = gasData; + heapConfig->napi_env = (void *)env; + heapConfig->fatal_function = fatal_handler; + + duk_context *ctx = duk_create_heap(duk_gas_respecting_alloc_function, duk_gas_respecting_realloc_function, duk_gas_respecting_free_function, heapConfig, (duk_fatal_function)fatal_handler); if (!ctx) { delete gasData; + delete heapConfig; napi_throw_error(env, nullptr, "Failed to create Duktape context"); return nullptr; } diff --git a/build/binding.sln b/build/binding.sln index 5c61bf7..7c7aa50 100644 --- a/build/binding.sln +++ b/build/binding.sln @@ -4,14 +4,14 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "duktape_bindings", "duktape EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 Release|x64 = Release|x64 + Debug|x64 = Debug|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {036ABA10-4771-9A05-C76B-5BBA4E082B13}.Debug|x64.ActiveCfg = Debug|x64 - {036ABA10-4771-9A05-C76B-5BBA4E082B13}.Debug|x64.Build.0 = Debug|x64 {036ABA10-4771-9A05-C76B-5BBA4E082B13}.Release|x64.ActiveCfg = Release|x64 {036ABA10-4771-9A05-C76B-5BBA4E082B13}.Release|x64.Build.0 = Release|x64 + {036ABA10-4771-9A05-C76B-5BBA4E082B13}.Debug|x64.ActiveCfg = Debug|x64 + {036ABA10-4771-9A05-C76B-5BBA4E082B13}.Debug|x64.Build.0 = Debug|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/build/config.gypi b/build/config.gypi index 452c4ee..60a728e 100644 --- a/build/config.gypi +++ b/build/config.gypi @@ -403,6 +403,18 @@ "want_separate_host_toolset": 0, "nodedir": "C:\\Users\\Nikita\\AppData\\Local\\node-gyp\\Cache\\18.17.0", "standalone_static_library": 1, - "msbuild_path": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe" + "msbuild_path": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe", + "access": "public", + "cache": "C:\\Users\\Nikita\\AppData\\Local\\npm-cache", + "globalconfig": "C:\\Users\\Nikita\\AppData\\Roaming\\npm\\etc\\npmrc", + "global_prefix": "C:\\Users\\Nikita\\AppData\\Roaming\\npm", + "init_module": "C:\\Users\\Nikita\\.npm-init.js", + "local_prefix": "C:\\Projects\\duktape-node", + "metrics_registry": "https://registry.npmjs.org/", + "node_gyp": "C:\\Users\\Nikita\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js", + "prefix": "C:\\Users\\Nikita\\AppData\\Roaming\\npm", + "unsafe_perm": "true", + "userconfig": "C:\\Users\\Nikita\\.npmrc", + "user_agent": "npm/8.19.2 node/v18.17.0 win32 x64 workspaces/false" } } diff --git a/build/duktape_bindings.vcxproj b/build/duktape_bindings.vcxproj index d894830..aaca8e7 100644 --- a/build/duktape_bindings.vcxproj +++ b/build/duktape_bindings.vcxproj @@ -149,7 +149,7 @@ $(IntDir)\bindings.obj - + diff --git a/build/duktape_bindings.vcxproj.filters b/build/duktape_bindings.vcxproj.filters index 84fb098..e24bb91 100644 --- a/build/duktape_bindings.vcxproj.filters +++ b/build/duktape_bindings.vcxproj.filters @@ -40,10 +40,16 @@ {56DF7A98-063D-FB9D-485C-089023B4C16A} - + + {741E0E76-39B2-B1AB-9FA1-F1A20B16F295} + + + {56DF7A98-063D-FB9D-485C-089023B4C16A} + + {77348C0E-2034-7791-74D5-63C077DF5A3B} - + {8CDEE807-BC53-E450-C8B8-4DEBB66742D4} @@ -63,8 +69,8 @@ .. - - C:\Users\Nikita\AppData\Roaming\npm\node_modules\node-gyp\src + + C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\src .. diff --git a/duktape b/duktape index 4b90322..ca81b3c 160000 --- a/duktape +++ b/duktape @@ -1 +1 @@ -Subproject commit 4b903220d5891899b904a61ac1537384a321f54d +Subproject commit ca81b3ccc062b19be391acb38b1a7edd4266278d diff --git a/index.js b/index.js index 81cbd4f..000f2dc 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,21 @@ const duktapeBindings = require('./build/Release/duktape_bindings.node'); class Glomium { - -} \ No newline at end of file + constructor(config) { + this.gasLimit = config?.gas?.limit || 100000; + this.memCostPerByte = config?.gas?.memoryByteCost || 1; + this.context = duktapeBindings.createContext({ gasLimit: this.gasLimit, memCostPerByte: this.memCostPerByte }) + return this; + } + set(name,value) { + duktapeBindings.setGlobal(this.context,name, value); + return this; + } + get(name) { + return duktapeBindings.getGlobal(this.context,name, value); + } + run(code) { + return duktapeBindings.evalString(this.context,code); + } +} +module.exports=Glomium \ No newline at end of file diff --git a/package.json b/package.json index d3e5ad1..38042f9 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "Duktape bindings for node.js", "main": "index.js", "scripts": { - "install": "node scripts/prebuild.js install" + "install": "node scripts/prebuild.js install", + "build:duktape:win32":"cd duktape &&C:\\Python27\\python.exe tools/configure.py --output-directory src-new --source-directory src-input --config-metadata config --option-file config/sandbox_config.yaml&&cd ..", + "build:win32":"npm run build:duktape:win32&&node-gyp rebuild" }, "repository": { "type": "git", diff --git a/test.js b/test.js new file mode 100644 index 0000000..f4894d7 --- /dev/null +++ b/test.js @@ -0,0 +1,12 @@ +const Glomium = require("./") + +console.log( + new Glomium({ + gas: { + limit: 1000000, + memoryByteCost: 1 + } + }) + .set("test", 5) + .run(`(test+2)`) +) \ No newline at end of file