-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a thin wrapper around napi_run_script. Refs: nodejs/node#15216 PR-URL: nodejs/node-addon-api#616 Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Kevin Eady <kevin.c.eady@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
1 parent
d7779b8
commit 332aa7e
Showing
8 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
Value RunPlainString(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
return env.RunScript("1 + 2 + 3"); | ||
} | ||
|
||
Value RunStdString(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
std::string str = "1 + 2 + 3"; | ||
return env.RunScript(str); | ||
} | ||
|
||
Value RunJsString(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
return env.RunScript(info[0].As<String>()); | ||
} | ||
|
||
Value RunWithContext(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
|
||
Array keys = info[1].As<Object>().GetPropertyNames(); | ||
std::string code = "("; | ||
for (unsigned int i = 0; i < keys.Length(); i++) { | ||
if (i != 0) code += ","; | ||
code += keys.Get(i).As<String>().Utf8Value(); | ||
} | ||
code += ") => " + info[0].As<String>().Utf8Value(); | ||
|
||
Value ret = env.RunScript(code); | ||
Function fn = ret.As<Function>(); | ||
std::vector<napi_value> args; | ||
for (unsigned int i = 0; i < keys.Length(); i++) { | ||
Value key = keys.Get(i); | ||
args.push_back(info[1].As<Object>().Get(key)); | ||
} | ||
return fn.Call(args); | ||
} | ||
|
||
} // end anonymous namespace | ||
|
||
Object InitRunScript(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["plainString"] = Function::New(env, RunPlainString); | ||
exports["stdString"] = Function::New(env, RunStdString); | ||
exports["jsString"] = Function::New(env, RunJsString); | ||
exports["withContext"] = Function::New(env, RunWithContext); | ||
|
||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const testUtil = require('./testUtil'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
testUtil.runGCTests([ | ||
'Plain C string', | ||
() => { | ||
const sum = binding.run_script.plainString(); | ||
assert.strictEqual(sum, 1 + 2 + 3); | ||
}, | ||
|
||
'std::string', | ||
() => { | ||
const sum = binding.run_script.stdString(); | ||
assert.strictEqual(sum, 1 + 2 + 3); | ||
}, | ||
|
||
'JavaScript string', | ||
() => { | ||
const sum = binding.run_script.jsString("1 + 2 + 3"); | ||
assert.strictEqual(sum, 1 + 2 + 3); | ||
}, | ||
|
||
'JavaScript, but not a string', | ||
() => { | ||
assert.throws(() => { | ||
binding.run_script.jsString(true); | ||
}, { | ||
name: 'Error', | ||
message: 'A string was expected' | ||
}); | ||
}, | ||
|
||
'With context', | ||
() => { | ||
const a = 1, b = 2, c = 3; | ||
const sum = binding.run_script.withContext("a + b + c", { a, b, c }); | ||
assert.strictEqual(sum, a + b + c); | ||
} | ||
]); | ||
} |