Skip to content
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

Move FunctionKind to CodeBlock #3440

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add IS_ARROW flag to codeblock
  • Loading branch information
HalidOdat committed Oct 30, 2023
commit 1ab803065474a68b3fbc4a00367d55d8df574973
15 changes: 5 additions & 10 deletions boa_engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,9 @@ impl BuiltInFunctionObject {
let environments = context.vm.environments.pop_to_global();

let function_object = if generator {
crate::vm::create_generator_function_object(code, r#async, Some(prototype), context)
crate::vm::create_generator_function_object(code, Some(prototype), context)
} else {
crate::vm::create_function_object(code, r#async, prototype, context)
crate::vm::create_function_object(code, prototype, context)
};

context.vm.environments.extend(environments);
Expand All @@ -588,12 +588,8 @@ impl BuiltInFunctionObject {
);

let environments = context.vm.environments.pop_to_global();
let function_object = crate::vm::create_generator_function_object(
code,
r#async,
Some(prototype),
context,
);
let function_object =
crate::vm::create_generator_function_object(code, Some(prototype), context);
context.vm.environments.extend(environments);

Ok(function_object)
Expand All @@ -610,8 +606,7 @@ impl BuiltInFunctionObject {
);

let environments = context.vm.environments.pop_to_global();
let function_object =
crate::vm::create_function_object(code, r#async, prototype, context);
let function_object = crate::vm::create_function_object(code, prototype, context);
context.vm.environments.extend(environments);

Ok(function_object)
Expand Down
8 changes: 4 additions & 4 deletions boa_engine/src/bytecompiler/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ impl ByteCompiler<'_, '_> {

// b. Let fo be InstantiateFunctionObject of f with arguments env and privateEnv.
let function = if generator {
create_generator_function_object(code, r#async, None, self.context)
create_generator_function_object(code, None, self.context)
} else {
create_function_object_fast(code, r#async, false, false, self.context)
create_function_object_fast(code, false, self.context)
};

// c. Perform ? env.CreateGlobalFunctionBinding(fn, fo, false).
Expand Down Expand Up @@ -737,9 +737,9 @@ impl ByteCompiler<'_, '_> {

// b. Let fo be InstantiateFunctionObject of f with arguments lexEnv and privateEnv.
let function = if generator {
create_generator_function_object(code, r#async, None, self.context)
create_generator_function_object(code, None, self.context)
} else {
create_function_object_fast(code, r#async, false, false, self.context)
create_function_object_fast(code, false, self.context)
};

// i. Perform ? varEnv.CreateGlobalFunctionBinding(fn, fo, true).
Expand Down
3 changes: 3 additions & 0 deletions boa_engine/src/bytecompiler/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ impl FunctionCompiler {
compiler
.code_block_flags
.set(CodeBlockFlags::IS_GENERATOR, self.generator);
compiler
.code_block_flags
.set(CodeBlockFlags::IS_ARROW, self.arrow);

if self.arrow {
compiler.this_mode = ThisMode::Lexical;
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/module/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,9 +1660,9 @@ impl SourceTextModule {
let code = codeblock.constant_function(index as usize);

let function = if kind.is_generator() {
create_generator_function_object(code, kind.is_async(), None, context)
create_generator_function_object(code, None, context)
} else {
create_function_object_fast(code, kind.is_async(), false, false, context)
create_function_object_fast(code, false, context)
};

context.vm.environments.put_lexical_value(
Expand Down
30 changes: 18 additions & 12 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ bitflags! {

const IS_ASYNC = 0b1000_0000;
const IS_GENERATOR = 0b0001_0000_0000;
const IS_ARROW = 0b0010_0000_0000;

/// Trace instruction execution to `stdout`.
#[cfg(feature = "trace")]
Expand Down Expand Up @@ -269,6 +270,11 @@ impl CodeBlock {
!self.is_async() && !self.is_generator()
}

/// Returns true if this function an arrow function.
pub(crate) fn is_arrow(&self) -> bool {
self.flags.get().contains(CodeBlockFlags::IS_ARROW)
}

/// Find exception [`Handler`] in the code block given the current program counter (`pc`).
#[inline]
pub(crate) fn find_handler(&self, pc: u32) -> Option<(usize, &Handler)> {
Expand Down Expand Up @@ -839,7 +845,6 @@ impl ToInternedString for CodeBlock {
/// This is slower than direct object template construction that is done in [`create_function_object_fast`].
pub(crate) fn create_function_object(
code: Gc<CodeBlock>,
r#async: bool,
prototype: JsObject,
context: &mut Context<'_>,
) -> JsObject {
Expand All @@ -850,18 +855,19 @@ pub(crate) fn create_function_object(

let script_or_module = context.get_active_script_or_module();

let is_async = code.is_async();
let function = OrdinaryFunction::new(
code,
context.vm.environments.clone(),
script_or_module,
context.realm().clone(),
);

let data = ObjectData::ordinary_function(function, !r#async);
let data = ObjectData::ordinary_function(function, !is_async);

let templates = context.intrinsics().templates();

let (mut template, storage, constructor_prototype) = if r#async {
let (mut template, storage, constructor_prototype) = if is_async {
(
templates.function_without_proto().clone(),
vec![length, name],
Expand Down Expand Up @@ -898,8 +904,6 @@ pub(crate) fn create_function_object(
/// with all the properties and prototype set.
pub(crate) fn create_function_object_fast(
code: Gc<CodeBlock>,
r#async: bool,
arrow: bool,
method: bool,
context: &mut Context<'_>,
) -> JsObject {
Expand All @@ -910,22 +914,24 @@ pub(crate) fn create_function_object_fast(

let script_or_module = context.get_active_script_or_module();

let is_async = code.is_async();
let is_arrow = code.is_arrow();
let function = OrdinaryFunction::new(
code,
context.vm.environments.clone(),
script_or_module,
context.realm().clone(),
);

let data = ObjectData::ordinary_function(function, !method && !arrow && !r#async);
let data = ObjectData::ordinary_function(function, !method && !is_arrow && !is_async);

if r#async {
if is_async {
context
.intrinsics()
.templates()
.async_function()
.create(data, vec![length, name])
} else if arrow || method {
} else if is_arrow || method {
context
.intrinsics()
.templates()
Expand Down Expand Up @@ -953,13 +959,13 @@ pub(crate) fn create_function_object_fast(
/// Creates a new generator function object.
pub(crate) fn create_generator_function_object(
code: Gc<CodeBlock>,
r#async: bool,
prototype: Option<JsObject>,
context: &mut Context<'_>,
) -> JsObject {
let is_async = code.is_async();
let function_prototype = if let Some(prototype) = prototype {
prototype
} else if r#async {
} else if is_async {
context
.intrinsics()
.constructors()
Expand Down Expand Up @@ -989,7 +995,7 @@ pub(crate) fn create_generator_function_object(

let prototype = JsObject::from_proto_and_data_with_shared_shape(
context.root_shape(),
if r#async {
if is_async {
context.intrinsics().objects().async_generator()
} else {
context.intrinsics().objects().generator()
Expand All @@ -999,7 +1005,7 @@ pub(crate) fn create_generator_function_object(

let script_or_module = context.get_active_script_or_module();

let constructor = if r#async {
let constructor = if is_async {
let function = OrdinaryFunction::new(
code,
context.vm.environments.clone(),
Expand Down
8 changes: 4 additions & 4 deletions boa_engine/src/vm/opcode/get/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl GetArrowFunction {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, false, true, false, context);
let function = create_function_object_fast(code, false, context);
context.vm.push(function);
Ok(CompletionType::Normal)
}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl GetAsyncArrowFunction {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, true, true, false, context);
let function = create_function_object_fast(code, false, context);
context.vm.push(function);
Ok(CompletionType::Normal)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ impl GetFunction {
method: bool,
) -> JsResult<CompletionType> {
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, false, false, method, context);
let function = create_function_object_fast(code, method, context);
context.vm.push(function);
Ok(CompletionType::Normal)
}
Expand Down Expand Up @@ -139,7 +139,7 @@ impl GetFunctionAsync {
method: bool,
) -> JsResult<CompletionType> {
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, true, false, method, context);
let function = create_function_object_fast(code, method, context);
context.vm.push(function);
Ok(CompletionType::Normal)
}
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/opcode/get/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl GetGenerator {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context.vm.frame().code_block().constant_function(index);
let function = create_generator_function_object(code, false, None, context);
let function = create_generator_function_object(code, None, context);
context.vm.push(function);
Ok(CompletionType::Normal)
}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl GetGeneratorAsync {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context.vm.frame().code_block().constant_function(index);
let function = create_generator_function_object(code, true, None, context);
let function = create_generator_function_object(code, None, context);
context.vm.push(function);
Ok(CompletionType::Normal)
}
Expand Down