2020-11-25 18:42:14 +09:00
|
|
|
use napi::{CallContext, JsObject, JsString, Result};
|
2020-08-03 15:34:58 +09:00
|
|
|
|
|
|
|
#[js_function(1)]
|
2020-09-03 20:11:30 +09:00
|
|
|
fn concat_string(ctx: CallContext) -> Result<JsString> {
|
2020-08-03 15:34:58 +09:00
|
|
|
let in_string = ctx.get::<JsString>(0)?;
|
2020-09-28 01:27:37 +09:00
|
|
|
let out_string = format!("{} + Rust 🦀 string!", in_string.into_utf8()?.as_str()?);
|
2020-08-03 15:34:58 +09:00
|
|
|
ctx.env.create_string_from_std(out_string)
|
|
|
|
}
|
2020-09-02 18:05:53 +09:00
|
|
|
|
2021-08-13 19:39:02 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
fn concat_utf16_string(ctx: CallContext) -> Result<JsString> {
|
|
|
|
let in_string = ctx.get::<JsString>(0)?;
|
|
|
|
let out_string = format!("{} + Rust 🦀 string!", in_string.into_utf16()?.as_str()?);
|
|
|
|
ctx.env.create_string_from_std(out_string)
|
|
|
|
}
|
|
|
|
|
2020-09-03 20:11:30 +09:00
|
|
|
#[js_function(1)]
|
|
|
|
fn concat_latin1_string(ctx: CallContext) -> Result<JsString> {
|
|
|
|
let in_string = ctx.get::<JsString>(0)?;
|
2020-09-28 01:27:37 +09:00
|
|
|
let out_string = format!(
|
|
|
|
"{} + Rust 🦀 string!",
|
|
|
|
in_string.into_latin1()?.into_latin1_string()?
|
|
|
|
);
|
2020-09-03 20:11:30 +09:00
|
|
|
ctx.env.create_string_from_std(out_string)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[js_function]
|
|
|
|
fn create_latin1(ctx: CallContext) -> Result<JsString> {
|
|
|
|
let bytes = vec![169, 191];
|
|
|
|
ctx.env.create_string_latin1(bytes.as_slice())
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
pub fn register_js(exports: &mut JsObject) -> Result<()> {
|
|
|
|
exports.create_named_method("concatString", concat_string)?;
|
2021-08-13 19:39:02 +09:00
|
|
|
exports.create_named_method("concatUTF16String", concat_utf16_string)?;
|
2020-11-25 18:42:14 +09:00
|
|
|
exports.create_named_method("concatLatin1String", concat_latin1_string)?;
|
|
|
|
exports.create_named_method("createLatin1", create_latin1)?;
|
2020-09-02 18:05:53 +09:00
|
|
|
Ok(())
|
|
|
|
}
|