diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /js/rust/tests/callback.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/rust/tests/callback.rs')
-rw-r--r-- | js/rust/tests/callback.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/js/rust/tests/callback.rs b/js/rust/tests/callback.rs new file mode 100644 index 0000000000..de75f97d26 --- /dev/null +++ b/js/rust/tests/callback.rs @@ -0,0 +1,79 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#[macro_use] +extern crate js; +extern crate libc; + +use js::ar::AutoRealm; +use js::glue::JSEncodeStringToUTF8; +use js::jsapi::root::JSContext; +use js::jsapi::root::JS_DefineFunction; +use js::jsapi::root::JS_NewGlobalObject; +use js::jsapi::root::JS_ReportErrorASCII; +use js::jsapi::root::JS::CallArgs; +use js::jsapi::root::JS::OnNewGlobalHookOption; +use js::jsapi::root::JS::RealmOptions; +use js::jsapi::root::JS::Value; +use js::jsval::UndefinedValue; +use js::rust::{Runtime, SIMPLE_GLOBAL_CLASS}; + +use std::ffi::CStr; +use std::ptr; +use std::str; + +#[test] +fn callback() { + let runtime = Runtime::new(false).unwrap(); + let context = runtime.cx(); + let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook; + let c_option = RealmOptions::default(); + + unsafe { + let global = JS_NewGlobalObject( + context, + &SIMPLE_GLOBAL_CLASS, + ptr::null_mut(), + h_option, + &c_option, + ); + rooted!(in(context) let global_root = global); + let global = global_root.handle(); + let _ar = AutoRealm::with_obj(context, global.get()); + let function = JS_DefineFunction( + context, + global, + b"puts\0".as_ptr() as *const libc::c_char, + Some(puts), + 1, + 0, + ); + assert!(!function.is_null()); + let javascript = "puts('Test Iñtërnâtiônàlizætiøn ┬─┬ノ( º _ ºノ) ');"; + rooted!(in(context) let mut rval = UndefinedValue()); + let _ = runtime.evaluate_script(global, javascript, "test.js", 0, rval.handle_mut()); + } +} + +unsafe extern "C" fn puts(context: *mut JSContext, argc: u32, vp: *mut Value) -> bool { + let args = CallArgs::from_vp(vp, argc); + + if args._base.argc_ != 1 { + JS_ReportErrorASCII( + context, + b"puts() requires exactly 1 argument\0".as_ptr() as *const libc::c_char, + ); + return false; + } + + let arg = args.get(0); + let js = js::rust::ToString(context, arg); + rooted!(in(context) let message_root = js); + let message = JSEncodeStringToUTF8(context, message_root.handle()); + let message = CStr::from_ptr(message); + println!("{}", str::from_utf8(message.to_bytes()).unwrap()); + + args.rval().set(UndefinedValue()); + return true; +} |