Skip to content

feat(engine): class private field #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions nova_vm/src/engine/bytecode/bytecode_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ impl<'a, 'gc, 'scope> CompileContext<'a, 'gc, 'scope> {
self.add_instruction(Instruction::PutValue);
}

/// Compile a class private field with an optional initializer into the
/// current context
pub(crate) fn compile_class_private_field(
&mut self,
private_identifier: &ast::PrivateIdentifier,
value: &Option<ast::Expression>,
) {
let identifier = String::from_str(self.agent, private_identifier.name.as_str(), self.gc);
// Turn the private name to a 'this' property access.
self.add_instruction(Instruction::ResolveThisBinding);
self.add_instruction_with_identifier(
Instruction::EvaluatePropertyAccessWithIdentifierKey,
identifier,
);

if let Some(value) = value {
// Minor optimisation: We do not need to push and pop the
// reference if we know we're not using the reference stack.
let is_literal = value.is_literal();
if !is_literal {
self.add_instruction(Instruction::PushReference);
}
value.compile(self);
if is_reference(value) {
self.add_instruction(Instruction::GetValue);
}
if !is_literal {
self.add_instruction(Instruction::PopReference);
}
} else {
// Same optimisation is unconditionally valid here.
self.add_instruction_with_constant(Instruction::StoreConstant, Value::Undefined);
}

self.add_instruction(Instruction::PutValue);
}

/// Compile a class computed field with an optional initializer into the
/// current context.
pub(crate) fn compile_class_computed_field(
Expand Down Expand Up @@ -1279,8 +1316,44 @@ impl CompileEvaluation for ast::StaticMemberExpression<'_> {
}

impl CompileEvaluation for ast::PrivateFieldExpression<'_> {
fn compile(&self, _ctx: &mut CompileContext) {
todo!()
fn compile(&self, ctx: &mut CompileContext) {
// 1. Let baseReference be ? Evaluation of MemberExpression.
self.object.compile(ctx);

// 2. Let baseValue be ? GetValue(baseReference).
if is_reference(&self.object) {
ctx.add_instruction(Instruction::GetValue);
}

if self.optional {
// Optional Chains

// Load copy of baseValue to stack.
ctx.add_instruction(Instruction::LoadCopy);
// 3. If baseValue is either undefined or null, then
ctx.add_instruction(Instruction::IsNullOrUndefined);
// a. Return undefined

// To return undefined we jump over the property access.
let jump_over_property_access =
ctx.add_instruction_with_jump_slot(Instruction::JumpIfTrue);

// Register our jump slot to the chain nullish case handling.
ctx.optional_chains
.as_mut()
.unwrap()
.push(jump_over_property_access);

// Return copy of baseValue from stack if it is not.
ctx.add_instruction(Instruction::Store);
}

// 4. Return EvaluatePropertyAccessWithIdentifierKey(baseValue, PrivateIdentifier, strict).
let identifier = String::from_str(ctx.agent, self.field.name.as_str(), ctx.gc);
ctx.add_instruction_with_identifier(
Instruction::EvaluatePropertyAccessWithIdentifierKey,
identifier,
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl CompileEvaluation for ast::AssignmentExpression<'_> {
ctx.add_instruction(Instruction::Store);
return;
}
ast::AssignmentTarget::PrivateFieldExpression(_) => todo!(),
ast::AssignmentTarget::PrivateFieldExpression(expression) => expression.compile(ctx),
ast::AssignmentTarget::StaticMemberExpression(expression) => {
expression.compile(ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
},
};
use ahash::{AHashMap, AHashSet};
use oxc_ast::ast::{self, MethodDefinitionKind};
use oxc_ast::ast::{self, MethodDefinitionKind, PropertyKey};
use oxc_ecmascript::{BoundNames, PrivateBoundIdentifiers, PropName};

use super::IndexType;
Expand Down Expand Up @@ -372,14 +372,21 @@ impl CompileEvaluation for ast::Class<'_> {
&property_definition.value,
);
} else {
let ast::PropertyKey::StaticIdentifier(key) = &property_definition.key
else {
unreachable!()
};
instance_fields.push(PropertyInitializerField::Static((
key,
&property_definition.value,
)));
match &property_definition.key {
ast::PropertyKey::StaticIdentifier(key) => {
instance_fields.push(PropertyInitializerField::Static((
key,
&property_definition.value,
)));
}
ast::PropertyKey::PrivateIdentifier(key) => {
instance_fields.push(PropertyInitializerField::Private((
key,
&property_definition.value,
)));
}
_ => unreachable!(),
}
}
}
ast::ClassElement::AccessorProperty(_) => todo!(),
Expand Down Expand Up @@ -444,6 +451,9 @@ impl CompileEvaluation for ast::Class<'_> {
PropertyInitializerField::Static((property_key, value)) => {
constructor_ctx.compile_class_static_field(property_key, value);
}
PropertyInitializerField::Private((property_key, value)) => {
constructor_ctx.compile_class_private_field(property_key, value);
}
PropertyInitializerField::Computed((key_id, value)) => {
constructor_ctx.compile_class_computed_field(key_id, value);
}
Expand Down Expand Up @@ -526,6 +536,12 @@ impl CompileEvaluation for ast::Class<'_> {
#[derive(Debug)]
enum PropertyInitializerField<'a, 'gc> {
Static((&'a ast::IdentifierName<'a>, &'a Option<ast::Expression<'a>>)),
Private(
(
&'a ast::PrivateIdentifier<'a>,
&'a Option<ast::Expression<'a>>,
),
),
Computed((String<'gc>, &'a Option<ast::Expression<'a>>)),
}

Expand Down Expand Up @@ -613,18 +629,28 @@ fn define_constructor_method(
/// at the top of the stack. The object is second on the stack.
fn define_method(class_element: &ast::MethodDefinition, ctx: &mut CompileContext) -> IndexType {
// 1. Let propKey be ? Evaluation of ClassElementName.
if let Some(prop_name) = class_element.prop_name() {
let prop_name = String::from_str(ctx.agent, prop_name.0, ctx.gc);
ctx.add_instruction_with_constant(Instruction::LoadConstant, prop_name);
} else {
// Computed method name.
let key = class_element.key.as_expression().unwrap();
key.compile(ctx);
if is_reference(key) {
ctx.add_instruction(Instruction::GetValue);
match &class_element.key {
PropertyKey::PrivateIdentifier(private_id) => {
let name = private_id.name;
let name = String::from_str(ctx.agent, &name, ctx.gc);
ctx.add_instruction_with_constant(Instruction::LoadConstant, name);
}
ctx.add_instruction(Instruction::Load);
};
_ => {
if let Some(prop_name) = class_element.prop_name() {
let prop_name = String::from_str(ctx.agent, prop_name.0, ctx.gc);
ctx.add_instruction_with_constant(Instruction::LoadConstant, prop_name);
} else {
// Computed method name.
let key = class_element.key.as_expression().unwrap();
key.compile(ctx);
if is_reference(key) {
ctx.add_instruction(Instruction::GetValue);
}
ctx.add_instruction(Instruction::Load);
};
}
}

// stack: [key, object]

// 2. Let env be the running execution context's LexicalEnvironment.
Expand Down
Loading