1- use wasm_bindgen:: prelude:: * ;
21use luars:: { LuaVM , LuaValue } ;
3- use std:: rc:: Rc ;
42use std:: cell:: RefCell ;
3+ use std:: rc:: Rc ;
4+ use wasm_bindgen:: prelude:: * ;
55
66// Set panic hook for better error messages in WASM
77#[ wasm_bindgen( start) ]
@@ -22,7 +22,7 @@ impl LuaWasm {
2222 pub fn new ( ) -> Result < LuaWasm , JsValue > {
2323 let mut vm = LuaVM :: new ( ) ;
2424 vm. open_libs ( ) ;
25-
25+
2626 Ok ( LuaWasm {
2727 vm : Rc :: new ( RefCell :: new ( vm) ) ,
2828 } )
@@ -32,14 +32,9 @@ impl LuaWasm {
3232 #[ wasm_bindgen]
3333 pub fn execute ( & self , code : & str ) -> Result < String , JsValue > {
3434 let mut vm = self . vm . borrow_mut ( ) ;
35-
36- match vm. compile ( code) {
37- Ok ( chunk) => {
38- match vm. execute ( Rc :: new ( chunk) ) {
39- Ok ( value) => Ok ( lua_value_to_string ( & value) ) ,
40- Err ( e) => Err ( JsValue :: from_str ( & format ! ( "Runtime error: {:?}" , e) ) ) ,
41- }
42- }
35+
36+ match vm. execute_string ( code) {
37+ Ok ( result) => Ok ( lua_value_to_string ( & result) ) ,
4338 Err ( e) => Err ( JsValue :: from_str ( & format ! ( "Compilation error: {:?}" , e) ) ) ,
4439 }
4540 }
@@ -48,8 +43,8 @@ impl LuaWasm {
4843 #[ wasm_bindgen( js_name = setGlobal) ]
4944 pub fn set_global ( & self , name : & str , value : JsValue ) -> Result < ( ) , JsValue > {
5045 let mut vm = self . vm . borrow_mut ( ) ;
51- let lua_value = js_value_to_lua ( & value )
52- . map_err ( |e| JsValue :: from_str ( & format ! ( "{:?}" , e) ) ) ?;
46+ let lua_value =
47+ js_value_to_lua ( & value ) . map_err ( |e| JsValue :: from_str ( & format ! ( "{:?}" , e) ) ) ?;
5348 vm. set_global ( name, lua_value) ;
5449 Ok ( ( ) )
5550 }
@@ -70,39 +65,43 @@ impl LuaWasm {
7065 pub fn eval ( & self , expr : & str ) -> Result < JsValue , JsValue > {
7166 let code = format ! ( "return {}" , expr) ;
7267 let mut vm = self . vm . borrow_mut ( ) ;
73-
68+
7469 match vm. compile ( & code) {
75- Ok ( chunk) => {
76- match vm. execute ( Rc :: new ( chunk) ) {
77- Ok ( value) => lua_value_to_js ( & value) ,
78- Err ( e) => Err ( JsValue :: from_str ( & format ! ( "Runtime error: {:?}" , e) ) ) ,
79- }
80- }
70+ Ok ( chunk) => match vm. execute ( Rc :: new ( chunk) ) {
71+ Ok ( value) => lua_value_to_js ( & value) ,
72+ Err ( e) => Err ( JsValue :: from_str ( & format ! ( "Runtime error: {:?}" , e) ) ) ,
73+ } ,
8174 Err ( e) => Err ( JsValue :: from_str ( & format ! ( "Compilation error: {:?}" , e) ) ) ,
8275 }
8376 }
84-
77+
8578 /// Register a simple JavaScript callback that can be called from Lua
8679 /// Note: This is a simplified version - full JS callback support requires additional work
8780 #[ wasm_bindgen( js_name = registerFunction) ]
88- pub fn register_function ( & self , name : String , _callback : js_sys:: Function ) -> Result < ( ) , JsValue > {
81+ pub fn register_function (
82+ & self ,
83+ name : String ,
84+ _callback : js_sys:: Function ,
85+ ) -> Result < ( ) , JsValue > {
8986 // For now, we'll create a placeholder function
9087 // Full JS callback support would require using thread_local storage
91- let code = format ! ( r#"
88+ let code = format ! (
89+ r#"
9290 function {}(...)
9391 error("JS callbacks not yet fully implemented - use setGlobal for values")
9492 end
95- "# , name) ;
96-
93+ "# ,
94+ name
95+ ) ;
96+
9797 let mut vm = self . vm . borrow_mut ( ) ;
98- let chunk = vm. compile ( & code) . map_err ( |e| {
99- JsValue :: from_str ( & format ! ( "Failed to register function: {:?}" , e) )
100- } ) ?;
101-
102- vm. execute ( Rc :: new ( chunk) ) . map_err ( |e| {
103- JsValue :: from_str ( & format ! ( "Failed to execute registration: {:?}" , e) )
104- } ) ?;
105-
98+ let chunk = vm
99+ . compile ( & code)
100+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to register function: {:?}" , e) ) ) ?;
101+
102+ vm. execute ( Rc :: new ( chunk) )
103+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to execute registration: {:?}" , e) ) ) ?;
104+
106105 Ok ( ( ) )
107106 }
108107}
0 commit comments