Skip to content

Commit

Permalink
update duktape, expose API
Browse files Browse the repository at this point in the history
  • Loading branch information
angrymouse committed Nov 8, 2023
1 parent a75bc2e commit 8ba3be0
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 19 deletions.
21 changes: 15 additions & 6 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<napi_env>(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;
}
Expand Down
6 changes: 3 additions & 3 deletions build/binding.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion build/config.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion build/duktape_bindings.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<ClCompile Include="..\bindings.cpp">
<ObjectFileName>$(IntDir)\bindings.obj</ObjectFileName>
</ClCompile>
<ClCompile Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\node-gyp\src\win_delay_load_hook.cc"/>
<ClCompile Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\src\win_delay_load_hook.cc"/>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets"/>
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets"/>
Expand Down
14 changes: 10 additions & 4 deletions build/duktape_bindings.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules">
<UniqueIdentifier>{56DF7A98-063D-FB9D-485C-089023B4C16A}</UniqueIdentifier>
</Filter>
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\node-gyp">
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm">
<UniqueIdentifier>{741E0E76-39B2-B1AB-9FA1-F1A20B16F295}</UniqueIdentifier>
</Filter>
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules">
<UniqueIdentifier>{56DF7A98-063D-FB9D-485C-089023B4C16A}</UniqueIdentifier>
</Filter>
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp">
<UniqueIdentifier>{77348C0E-2034-7791-74D5-63C077DF5A3B}</UniqueIdentifier>
</Filter>
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\node-gyp\src">
<Filter Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\src">
<UniqueIdentifier>{8CDEE807-BC53-E450-C8B8-4DEBB66742D4}</UniqueIdentifier>
</Filter>
<Filter Include="..">
Expand All @@ -63,8 +69,8 @@
<ClCompile Include="..\bindings.cpp">
<Filter>..</Filter>
</ClCompile>
<ClCompile Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\node-gyp\src\win_delay_load_hook.cc">
<Filter>C:\Users\Nikita\AppData\Roaming\npm\node_modules\node-gyp\src</Filter>
<ClCompile Include="C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\src\win_delay_load_hook.cc">
<Filter>C:\Users\Nikita\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\src</Filter>
</ClCompile>
<None Include="..\binding.gyp">
<Filter>..</Filter>
Expand Down
2 changes: 1 addition & 1 deletion duktape
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const duktapeBindings = require('./build/Release/duktape_bindings.node');

class Glomium {

}
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Glomium = require("./")

console.log(
new Glomium({
gas: {
limit: 1000000,
memoryByteCost: 1
}
})
.set("test", 5)
.run(`(test+2)`)
)

0 comments on commit 8ba3be0

Please sign in to comment.