mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Correct default export names and namespace symbol assignments
This commit is contained in:
3 files changed
+54
-4
No files matched your search
@@ -492,7 +492,10 @@ impl<'a> FnCompiler<'a> {
|
||||
fn lexical_decls(&mut self, decls: &[VarDecl]) -> CResult<()> {
|
||||
for d in decls {
|
||||
match (d.init, &d.target) {
|
||||
(Some(init), Binding::Name(name)) => self.named_expr(init, &name.name)?,
|
||||
(Some(init), Binding::Name(name)) => {
|
||||
let public_name = if name.name == crate::ast::DEFAULT_BINDING { "default" } else { &name.name };
|
||||
self.named_expr(init, public_name)?;
|
||||
}
|
||||
(Some(init), Binding::Pattern(_)) => self.expr(init)?,
|
||||
(None, Binding::Name(_)) => self.emit(Op::Undefined),
|
||||
(None, Binding::Pattern(_)) => return internal("a destructuring declaration without an initializer"),
|
||||
@@ -2247,7 +2250,10 @@ pub(crate) fn compile_function(ast: &Ast, fid: FunctionId, source: &Rc<Source>,
|
||||
arrow: f.kind == FunctionKind::Arrow,
|
||||
param_init,
|
||||
pattern_names,
|
||||
own_name: es2015.then(|| key_of(f.name.as_ref().map_or("", |n| n.name.as_str()))),
|
||||
own_name: es2015.then(|| {
|
||||
let name = f.name.as_ref().map_or("", |n| n.name.as_str());
|
||||
key_of(if f.kind == FunctionKind::Declaration && name == crate::ast::DEFAULT_BINDING { "default" } else { name })
|
||||
}),
|
||||
method: es2015 && matches!(f.kind, FunctionKind::Method | FunctionKind::Getter | FunctionKind::Setter),
|
||||
};
|
||||
c.finish(hoisted, meta)
|
||||
|
||||
@@ -11,6 +11,31 @@ fn es() -> Realm {
|
||||
realm
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_exported_functions_keep_their_public_names() {
|
||||
for (source, name) in [
|
||||
("export default function () {}", "default"),
|
||||
("export default () => {};", "default"),
|
||||
("export default (function () {});", "default"),
|
||||
("export default function named() {}", "named"),
|
||||
] {
|
||||
let out = run("import value from './m.mjs'; out.push(value.name);", &[("./m.mjs", source)]);
|
||||
assert_eq!(out, name, "{source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbol_writes_through_namespace_prototypes_are_refused() {
|
||||
let out = run(
|
||||
"import * as ns from './m.mjs'; var k = Symbol('new'), child = Object.create(ns); \
|
||||
try { child[k] = 1; } catch (e) { out.push(e.name); } \
|
||||
out.push(Object.getOwnPropertySymbols(child).length); \
|
||||
Object.defineProperty(child, k, {value: 3, writable: true}); child[k] = 4; out.push(child[k]);",
|
||||
&[("./m.mjs", "export var value = 1;")],
|
||||
);
|
||||
assert_eq!(out, "TypeError|0|4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_namespace_exposes_its_own_immutable_symbol_tag() {
|
||||
let out = run(
|
||||
|
||||
+21
-2
@@ -394,8 +394,27 @@ impl Interp {
|
||||
Some(Prop::Accessor { set: None, .. }) => return self.reject_sym(throw, &k, "it has only a getter"),
|
||||
None => {}
|
||||
}
|
||||
let proto = self.object(id)?.proto;
|
||||
match proto.map(|p| self.find_sym(p, &k)).transpose()?.flatten() {
|
||||
let mut cur = self.object(id)?.proto;
|
||||
let mut inherited = None;
|
||||
let mut steps = 0usize;
|
||||
while let Some(object) = cur {
|
||||
if matches!(self.object(object)?.class, crate::heap::Class::ModuleNamespace(_)) {
|
||||
return self.reject_sym(throw, &k, "a module namespace object is read-only");
|
||||
}
|
||||
if let Some(property) = self.get_own_sym(object, &k)? {
|
||||
inherited = Some(property);
|
||||
break;
|
||||
}
|
||||
cur = self.object(object)?.proto;
|
||||
if cur.is_some() {
|
||||
self.use_fuel()?;
|
||||
}
|
||||
steps += 1;
|
||||
if steps > 1_000_000 {
|
||||
return Err(crate::interp::Abrupt::Internal("prototype chain too long".into()));
|
||||
}
|
||||
}
|
||||
match inherited {
|
||||
Some(Prop::Accessor { set: Some(s), .. }) => return self.call(&Value::Object(s), &Value::Object(id), &[v]).map(drop),
|
||||
Some(Prop::Accessor { set: None, .. }) => return self.reject_sym(throw, &k, "it has only a getter"),
|
||||
Some(Prop::Data { flags, .. }) if flags & WRITABLE == 0 => return self.reject_sym(throw, &k, "it is read-only"),
|
||||
|
||||
Reference in new issue
Block a user