Merge pull request #611 from napi-rs/env-test
test(napi): access env variable in native
This commit is contained in:
commit
0b2b03e009
2 changed files with 16 additions and 1 deletions
7
test_module/__test__/env.spec.ts
Normal file
7
test_module/__test__/env.spec.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import test from 'ava'
|
||||||
|
|
||||||
|
const bindings = require('../index.node')
|
||||||
|
|
||||||
|
test('should be able to access env variable from native', (t) => {
|
||||||
|
t.is(bindings.getEnvVariable(), 'napi-rs')
|
||||||
|
})
|
|
@ -1,4 +1,4 @@
|
||||||
use napi::{CallContext, JsBoolean, JsObject, JsUnknown, Result};
|
use napi::{CallContext, ContextlessResult, Env, JsBoolean, JsObject, JsString, JsUnknown, Result};
|
||||||
|
|
||||||
#[js_function(2)]
|
#[js_function(2)]
|
||||||
pub fn instanceof(ctx: CallContext) -> Result<JsBoolean> {
|
pub fn instanceof(ctx: CallContext) -> Result<JsBoolean> {
|
||||||
|
@ -32,11 +32,19 @@ pub fn cast_unknown(ctx: CallContext) -> Result<JsObject> {
|
||||||
Ok(unsafe { arg.cast::<JsObject>() })
|
Ok(unsafe { arg.cast::<JsObject>() })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[contextless_function]
|
||||||
|
fn get_env_variable(env: Env) -> ContextlessResult<JsString> {
|
||||||
|
env
|
||||||
|
.create_string_from_std(std::env::var("npm_package_name").unwrap())
|
||||||
|
.map(Some)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn register_js(exports: &mut JsObject) -> Result<()> {
|
pub fn register_js(exports: &mut JsObject) -> Result<()> {
|
||||||
exports.create_named_method("instanceof", instanceof)?;
|
exports.create_named_method("instanceof", instanceof)?;
|
||||||
exports.create_named_method("isTypedarray", is_typedarray)?;
|
exports.create_named_method("isTypedarray", is_typedarray)?;
|
||||||
exports.create_named_method("isDataview", is_dataview)?;
|
exports.create_named_method("isDataview", is_dataview)?;
|
||||||
exports.create_named_method("strictEquals", strict_equals)?;
|
exports.create_named_method("strictEquals", strict_equals)?;
|
||||||
exports.create_named_method("castUnknown", cast_unknown)?;
|
exports.create_named_method("castUnknown", cast_unknown)?;
|
||||||
|
exports.create_named_method("getEnvVariable", get_env_variable)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue