mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Include static in class assembly and bytecode
This commit is contained in:
@@ -139,25 +139,55 @@ impl std::fmt::Display for Function {
|
||||
pub struct Class {
|
||||
pub constructor: Value,
|
||||
pub prototype: Value,
|
||||
pub static_: Value,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Class {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "class({}, ", self.constructor)?;
|
||||
writeln!(f, "class {{")?;
|
||||
|
||||
writeln!(f, " constructor: {},", self.constructor)?;
|
||||
|
||||
write!(f, " prototype: ")?;
|
||||
|
||||
match &self.prototype {
|
||||
Value::Object(object) => {
|
||||
write!(f, "{{\n")?;
|
||||
for (name, method) in &object.properties {
|
||||
write!(f, " {}: {},\n", name, method)?;
|
||||
if object.properties.len() == 0 {
|
||||
writeln!(f, "{{}},")?;
|
||||
} else {
|
||||
write!(f, "{{\n")?;
|
||||
for (name, method) in &object.properties {
|
||||
write!(f, " {}: {},\n", name, method)?;
|
||||
}
|
||||
writeln!(f, " }},")?;
|
||||
}
|
||||
write!(f, "}})")?;
|
||||
}
|
||||
_ => {
|
||||
write!(f, "{})", self.prototype)?;
|
||||
writeln!(f, "{},", self.prototype)?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, " static: ")?;
|
||||
|
||||
match &self.static_ {
|
||||
Value::Object(object) => {
|
||||
if object.properties.len() == 0 {
|
||||
writeln!(f, "{{}},")?;
|
||||
} else {
|
||||
write!(f, "{{\n")?;
|
||||
for (name, method) in &object.properties {
|
||||
write!(f, " {}: {},\n", name, method)?;
|
||||
}
|
||||
writeln!(f, " }},")?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
writeln!(f, "{},", self.prototype)?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "}}")?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,6 +147,7 @@ impl Assembler {
|
||||
self.output.push(ValueType::Class as u8);
|
||||
self.value(&class.constructor);
|
||||
self.value(&class.prototype);
|
||||
self.value(&class.static_);
|
||||
}
|
||||
|
||||
fn label(&mut self, label: &Label) {
|
||||
|
||||
@@ -514,23 +514,30 @@ impl<'a> AssemblyParser<'a> {
|
||||
}
|
||||
|
||||
fn assemble_class(&mut self) -> Class {
|
||||
self.parse_exact("class(");
|
||||
self.parse_exact("class {");
|
||||
self.parse_optional_whitespace();
|
||||
|
||||
self.parse_exact("constructor: ");
|
||||
let constructor = self.assemble_value();
|
||||
self.parse_optional_whitespace();
|
||||
|
||||
self.parse_exact(",");
|
||||
self.parse_optional_whitespace();
|
||||
|
||||
let methods = self.assemble_value();
|
||||
self.parse_exact("prototype: ");
|
||||
let prototype = self.assemble_value();
|
||||
self.parse_exact(",");
|
||||
self.parse_optional_whitespace();
|
||||
|
||||
self.parse_exact(")");
|
||||
self.parse_exact("static: ");
|
||||
let static_ = self.assemble_value();
|
||||
self.parse_exact(",");
|
||||
self.parse_optional_whitespace();
|
||||
|
||||
self.parse_exact("}");
|
||||
|
||||
Class {
|
||||
constructor,
|
||||
prototype: methods,
|
||||
prototype,
|
||||
static_,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -673,7 +673,7 @@ impl ModuleCompiler {
|
||||
class: &swc_ecma_ast::Class,
|
||||
) -> Pointer {
|
||||
let mut constructor: Value = Value::Void;
|
||||
let mut methods: Object = Object::default();
|
||||
let mut prototype: Object = Object::default();
|
||||
let mut dependent_definitions: Vec<Definition>;
|
||||
|
||||
let defn_name = match ident {
|
||||
@@ -820,7 +820,7 @@ impl ModuleCompiler {
|
||||
Functionish::Fn(None, method.function.clone()),
|
||||
));
|
||||
|
||||
methods
|
||||
prototype
|
||||
.properties
|
||||
.push((name, Value::Pointer(method_defn_name)));
|
||||
}
|
||||
@@ -847,7 +847,8 @@ impl ModuleCompiler {
|
||||
pointer: defn_name.clone(),
|
||||
content: DefinitionContent::Class(Class {
|
||||
constructor,
|
||||
prototype: Value::Object(Box::new(methods)),
|
||||
prototype: Value::Object(Box::new(prototype)),
|
||||
static_: Value::Object(Box::new(Object::default())),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user