Allow global functions in non-aliased imports.

This commit is contained in:
Stephen Chung 2022-10-14 12:14:28 +08:00
parent 31d045279f
commit b65b7b05a5
3 changed files with 28 additions and 23 deletions

View File

@ -9,6 +9,7 @@ Bug fixes
* `Engine::parse_json` now returns an error on unquoted keys to be consistent with JSON specifications. * `Engine::parse_json` now returns an error on unquoted keys to be consistent with JSON specifications.
* `import` statements inside `eval` no longer cause errors in subsequent code. * `import` statements inside `eval` no longer cause errors in subsequent code.
* Functions marked `global` in `import`ed modules with no alias names now work properly.
New features New features
------------ ------------

View File

@ -952,15 +952,19 @@ impl Engine {
}); });
if let Ok(module) = module_result { if let Ok(module) = module_result {
if !export.is_empty() { let (export, must_be_indexed) = if !export.is_empty() {
if module.is_indexed() { (export.name.clone(), true)
global.push_import(export.name.clone(), module); } else {
} else { (self.get_interned_string(""), false)
// Index the module (making a clone copy if necessary) if it is not indexed };
let mut m = crate::func::shared_take_or_clone(module);
m.build_index(); if !must_be_indexed || module.is_indexed() {
global.push_import(export.name.clone(), m); global.push_import(export, module);
} } else {
// Index the module (making a clone copy if necessary) if it is not indexed
let mut m = crate::func::shared_take_or_clone(module);
m.build_index();
global.push_import(export, m);
} }
global.num_modules_loaded += 1; global.num_modules_loaded += 1;

View File

@ -2993,24 +2993,24 @@ impl Engine {
// import expr ... // import expr ...
let expr = self.parse_expr(input, state, lib, settings.level_up())?; let expr = self.parse_expr(input, state, lib, settings.level_up())?;
// import expr; let export = if !match_token(input, Token::As).0 {
if !match_token(input, Token::As).0 { // import expr;
let empty = Ident { Ident {
name: state.get_interned_string(""), name: state.get_interned_string(""),
pos: Position::NONE, pos: Position::NONE,
}; }
return Ok(Stmt::Import((expr, empty).into(), settings.pos)); } else {
} // import expr as name ...
let (name, pos) = parse_var_name(input)?;
Ident {
name: state.get_interned_string(name),
pos,
}
};
// import expr as name ... state.imports.push(export.name.clone());
let (name, pos) = parse_var_name(input)?;
let name = state.get_interned_string(name);
state.imports.push(name.clone());
Ok(Stmt::Import( Ok(Stmt::Import((expr, export).into(), settings.pos))
(expr, Ident { name, pos }).into(),
settings.pos,
))
} }
/// Parse an export statement. /// Parse an export statement.