@@ -14,12 +14,24 @@ pub struct JsRuntimeState {
1414 pub commonjs_modules : HashMap < String , v8:: Global < v8:: Value > > ,
1515}
1616
17+ #[ derive( Debug ) ]
18+ pub struct ScriptExecutionErrorData {
19+ pub file_name : String ,
20+ pub line_number : u64 ,
21+ pub start_column : u64 ,
22+ pub end_column : u64 ,
23+ pub source_line : String ,
24+ pub trace : String ,
25+ pub message : String ,
26+ }
27+
1728#[ derive( Debug ) ]
1829pub enum Error {
1930 JSRuntimeError ,
2031 ExecutionTimeout ,
2132 MemoryLimitExceeded ,
2233 V8Error ,
34+ ScriptExecutionError ( ScriptExecutionErrorData ) ,
2335}
2436
2537fn init_v8 ( ) {
@@ -159,8 +171,11 @@ impl JSRuntime {
159171 false ,
160172 false ,
161173 ) ;
174+
175+ let try_catch = & mut v8:: TryCatch :: new ( scope) ;
176+
162177 let script =
163- v8:: Script :: compile ( scope , code, Some ( & script_origin) ) . ok_or ( Error :: JSRuntimeError ) ?;
178+ v8:: Script :: compile ( try_catch , code, Some ( & script_origin) ) . ok_or ( Error :: JSRuntimeError ) ?;
164179 let stop_flag = std:: sync:: Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
165180 let time_limit_hit = std:: sync:: Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
166181 let memory_limit_hit = std:: sync:: Arc :: new ( std:: sync:: atomic:: AtomicBool :: new ( false ) ) ;
@@ -217,7 +232,7 @@ impl JSRuntime {
217232 } ) ;
218233 }
219234
220- let result = script. run ( scope ) ;
235+ let result = script. run ( try_catch ) ;
221236 stop_flag. store ( true , std:: sync:: atomic:: Ordering :: SeqCst ) ;
222237 let time_limit_hit = time_limit_hit. load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
223238 let memory_limit_hit = memory_limit_hit. load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
@@ -237,8 +252,24 @@ impl JSRuntime {
237252
238253 stop_flag. store ( true , std:: sync:: atomic:: Ordering :: SeqCst ) ;
239254 let result = match result {
240- Some ( result) => Ok ( Some ( v8:: Global :: new ( scope, result) ) ) ,
241- None => Ok ( None ) ,
255+ Some ( result) => Ok ( Some ( v8:: Global :: new ( try_catch, result) ) ) ,
256+ None => {
257+ let exception = try_catch. exception ( ) . unwrap ( ) ;
258+ let exception_string = exception
259+ . to_string ( try_catch)
260+ . unwrap ( )
261+ . to_rust_string_lossy ( try_catch) ;
262+ let message = try_catch. message ( ) . unwrap ( ) ;
263+ Err ( Error :: ScriptExecutionError ( ScriptExecutionErrorData {
264+ file_name : message. get_script_resource_name ( try_catch) . unwrap ( ) . to_rust_string_lossy ( try_catch) ,
265+ line_number : u64:: try_from ( message. get_line_number ( try_catch) . unwrap ( ) ) . unwrap ( ) ,
266+ start_column : u64:: try_from ( message. get_start_column ( ) ) . unwrap ( ) ,
267+ end_column : u64:: try_from ( message. get_end_column ( ) ) . unwrap ( ) ,
268+ trace : "" . into ( ) , // todo,
269+ message : exception_string,
270+ source_line : message. get_source_line ( try_catch) . unwrap ( ) . to_rust_string_lossy ( try_catch) ,
271+ } ) )
272+ }
242273 } ;
243274 result
244275 }
0 commit comments