44
55use std:: ffi:: CStr ;
66use std:: ptr;
7+ use std:: ptr:: NonNull ;
78use std:: str;
89
9- use mozjs:: glue:: EncodeStringToUTF8 ;
10- use mozjs:: jsapi:: { CallArgs , JSAutoRealm , JSContext , OnNewGlobalHookOption , Value } ;
11- use mozjs:: jsapi:: { JS_DefineFunction , JS_NewGlobalObject , JS_ReportErrorASCII } ;
10+ use mozjs:: context:: { JSContext , RawJSContext } ;
11+ use mozjs:: jsapi:: { CallArgs , JSAutoRealm , JS_ReportErrorASCII , OnNewGlobalHookOption , Value } ;
1212use mozjs:: jsval:: UndefinedValue ;
1313use mozjs:: rooted;
14+ use mozjs:: rust:: wrappers2:: { EncodeStringToUTF8 , JS_DefineFunction , JS_NewGlobalObject } ;
1415use mozjs:: rust:: { JSEngine , RealmOptions , Runtime , SIMPLE_GLOBAL_CLASS } ;
1516
1617#[ test]
1718fn callback ( ) {
1819 let engine = JSEngine :: init ( ) . unwrap ( ) ;
1920 let mut runtime = Runtime :: new ( engine. handle ( ) ) ;
20- let context = * * runtime. cx ( ) ;
21+ let context = runtime. cx ( ) ;
2122 #[ cfg( feature = "debugmozjs" ) ]
2223 unsafe {
23- mozjs:: jsapi:: SetGCZeal ( context, 2 , 1 ) ;
24+ mozjs:: jsapi:: SetGCZeal ( context. raw_cx ( ) , 2 , 1 ) ;
2425 }
2526 let h_option = OnNewGlobalHookOption :: FireOnNewGlobalHook ;
2627 let c_option = RealmOptions :: default ( ) ;
2728
2829 unsafe {
29- rooted ! ( in( context) let global = JS_NewGlobalObject (
30+ rooted ! ( & in( context) let global = JS_NewGlobalObject (
3031 context,
3132 & SIMPLE_GLOBAL_CLASS ,
3233 ptr:: null_mut( ) ,
3334 h_option,
3435 & * c_option,
3536 ) ) ;
36- let _ac = JSAutoRealm :: new ( context, global. get ( ) ) ;
37+ let _ac = JSAutoRealm :: new ( context. raw_cx ( ) , global. get ( ) ) ;
3738
3839 let function = JS_DefineFunction (
3940 context,
@@ -46,32 +47,39 @@ fn callback() {
4647 assert ! ( !function. is_null( ) ) ;
4748
4849 let javascript = "puts('Test Iñtërnâtiônàlizætiøn ┬─┬ノ( º _ ºノ) ');" ;
49- rooted ! ( in( context) let mut rval = UndefinedValue ( ) ) ;
50+ rooted ! ( & in( context) let mut rval = UndefinedValue ( ) ) ;
51+ // Rust automagically drops context here, so we can continue to use runtime here
5052 let options = runtime. new_compile_options ( "test.js" , 0 ) ;
5153 assert ! ( runtime
5254 . evaluate_script( global. handle( ) , javascript, rval. handle_mut( ) , options)
5355 . is_ok( ) ) ;
5456 }
5557}
5658
57- unsafe extern "C" fn puts ( context : * mut JSContext , argc : u32 , vp : * mut Value ) -> bool {
59+ unsafe extern "C" fn puts ( context : * mut RawJSContext , argc : u32 , vp : * mut Value ) -> bool {
60+ // SAFETY: This is safe because we are in callback (so this is only access to context)
61+ // and we shadow the ptr, so it cannot be used anymore
62+ let mut context = JSContext :: from_ptr ( NonNull :: new ( context) . unwrap ( ) ) ;
5863 let args = CallArgs :: from_vp ( vp, argc) ;
5964
6065 if args. argc_ != 1 {
61- JS_ReportErrorASCII ( context, c"puts() requires exactly 1 argument" . as_ptr ( ) ) ;
66+ JS_ReportErrorASCII (
67+ context. raw_cx ( ) ,
68+ c"puts() requires exactly 1 argument" . as_ptr ( ) ,
69+ ) ;
6270 return false ;
6371 }
6472
6573 let arg = mozjs:: rust:: Handle :: from_raw ( args. get ( 0 ) ) ;
66- let js = mozjs:: rust:: ToString ( context, arg) ;
67- rooted ! ( in( context) let message_root = js) ;
74+ let js = mozjs:: rust:: ToString ( context. raw_cx ( ) , arg) ;
75+ rooted ! ( & in( context) let message_root = js) ;
6876 unsafe extern "C" fn cb ( message : * const core:: ffi:: c_char ) {
6977 let message = CStr :: from_ptr ( message) ;
7078 let message = str:: from_utf8 ( message. to_bytes ( ) ) . unwrap ( ) ;
7179 assert_eq ! ( message, "Test Iñtërnâtiônàlizætiøn ┬─┬ノ( º _ ºノ) " ) ;
7280 println ! ( "{}" , message) ;
7381 }
74- EncodeStringToUTF8 ( context, message_root. handle ( ) . into ( ) , cb) ;
82+ EncodeStringToUTF8 ( & mut context, message_root. handle ( ) . into ( ) , cb) ;
7583
7684 args. rval ( ) . set ( UndefinedValue ( ) ) ;
7785 true
0 commit comments