|
1 | 1 | use crate::browser_module::setup_browser_module;
|
2 | 2 | use crate::convert;
|
3 | 3 | use crate::wasm_builtins;
|
4 |
| -use js_sys::{Object, SyntaxError, TypeError}; |
| 4 | +use js_sys::{Object, Reflect, SyntaxError, TypeError}; |
5 | 5 | use rustpython_vm::{
|
6 | 6 | compile,
|
7 | 7 | frame::ScopeRef,
|
@@ -318,10 +318,44 @@ impl WASMVirtualMachine {
|
318 | 318 | }| {
|
319 | 319 | source.push('\n');
|
320 | 320 | let code =
|
321 |
| - compile::compile(&source, &mode, "<wasm>".to_string(), vm.ctx.code_type()) |
322 |
| - .map_err(|err| { |
323 |
| - SyntaxError::new(&format!("Error parsing Python code: {}", err)) |
324 |
| - })?; |
| 321 | + compile::compile(&source, &mode, "<wasm>".to_string(), vm.ctx.code_type()); |
| 322 | + let code = code.map_err(|err| { |
| 323 | + let js_err = SyntaxError::new(&format!("Error parsing Python code: {}", err)); |
| 324 | + if let rustpython_vm::error::CompileError::Parse(ref parse_error) = err { |
| 325 | + use rustpython_parser::error::ParseError; |
| 326 | + if let ParseError::EOF(Some(ref loc)) |
| 327 | + | ParseError::ExtraToken((ref loc, ..)) |
| 328 | + | ParseError::InvalidToken(ref loc) |
| 329 | + | ParseError::UnrecognizedToken((ref loc, ..), _) = parse_error |
| 330 | + { |
| 331 | + let _ = Reflect::set( |
| 332 | + &js_err, |
| 333 | + &"row".into(), |
| 334 | + &(loc.get_row() as u32).into(), |
| 335 | + ); |
| 336 | + let _ = Reflect::set( |
| 337 | + &js_err, |
| 338 | + &"col".into(), |
| 339 | + &(loc.get_column() as u32).into(), |
| 340 | + ); |
| 341 | + } |
| 342 | + if let ParseError::ExtraToken((_, _, ref loc)) |
| 343 | + | ParseError::UnrecognizedToken((_, _, ref loc), _) = parse_error |
| 344 | + { |
| 345 | + let _ = Reflect::set( |
| 346 | + &js_err, |
| 347 | + &"endrow".into(), |
| 348 | + &(loc.get_row() as u32).into(), |
| 349 | + ); |
| 350 | + let _ = Reflect::set( |
| 351 | + &js_err, |
| 352 | + &"endcol".into(), |
| 353 | + &(loc.get_column() as u32).into(), |
| 354 | + ); |
| 355 | + } |
| 356 | + } |
| 357 | + js_err |
| 358 | + })?; |
325 | 359 | let result = vm.run_code_obj(code, scope.clone());
|
326 | 360 | convert::pyresult_to_jsresult(vm, result)
|
327 | 361 | },
|
|
0 commit comments