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

Simplify all extensions APIs of Context #3456

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
10 changes: 5 additions & 5 deletions boa_cli/src/debug/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn flowgraph_parse_direction_option(value: &JsValue) -> JsResult<Direction> {
}

/// Get functions instruction flowgraph
fn flowgraph(_this: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn flowgraph(_this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let Some(value) = args.get(0) else {
return Err(JsNativeError::typ()
.with_message("expected function argument")
Expand Down Expand Up @@ -101,7 +101,7 @@ fn flowgraph(_this: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> Js
Ok(JsValue::new(js_string!(result)))
}

fn bytecode(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn bytecode(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let Some(value) = args.get(0) else {
return Err(JsNativeError::typ()
.with_message("expected function argument")
Expand Down Expand Up @@ -137,7 +137,7 @@ fn set_trace_flag_in_function_object(object: &JsObject, value: bool) -> JsResult
}

/// Trace function.
fn trace(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn trace(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0);
let this = args.get_or_undefined(1);

Expand All @@ -156,7 +156,7 @@ fn trace(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<J
result
}

fn traceable(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn traceable(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0);
let traceable = args.get_or_undefined(1).to_boolean();

Expand All @@ -171,7 +171,7 @@ fn traceable(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsV
Ok(value.clone())
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
ObjectInitializer::new(context)
.function(
NativeFunction::from_fn_ptr(flowgraph),
Expand Down
4 changes: 2 additions & 2 deletions boa_cli/src/debug/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use boa_engine::{
};

/// Trigger garbage collection.
fn collect(_: &JsValue, _: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn collect(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
boa_gc::force_collect();
Ok(JsValue::undefined())
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
ObjectInitializer::new(context)
.function(
NativeFunction::from_fn_ptr(collect),
Expand Down
14 changes: 7 additions & 7 deletions boa_cli/src/debug/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ use boa_engine::{
Context, JsArgs, JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
};

fn get_loop(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn get_loop(_: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let max = context.runtime_limits().loop_iteration_limit();
Ok(JsValue::from(max))
}

fn set_loop(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn set_loop(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0).to_length(context)?;
context.runtime_limits_mut().set_loop_iteration_limit(value);
Ok(JsValue::undefined())
}

fn get_stack(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn get_stack(_: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let max = context.runtime_limits().stack_size_limit();
Ok(JsValue::from(max))
}

fn set_stack(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn set_stack(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0).to_length(context)?;
let Ok(value) = value.try_into() else {
return Err(JsNativeError::range()
Expand All @@ -32,12 +32,12 @@ fn set_stack(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResu
Ok(JsValue::undefined())
}

fn get_recursion(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn get_recursion(_: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let max = context.runtime_limits().recursion_limit();
Ok(JsValue::from(max))
}

fn set_recursion(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn set_recursion(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0).to_length(context)?;
let Ok(value) = value.try_into() else {
return Err(JsNativeError::range()
Expand All @@ -48,7 +48,7 @@ fn set_recursion(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> Js
Ok(JsValue::undefined())
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
let get_loop =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(get_loop))
.name("get loop")
Expand Down
4 changes: 2 additions & 2 deletions boa_cli/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod optimizer;
mod realm;
mod shape;

fn create_boa_object(context: &mut Context<'_>) -> JsObject {
fn create_boa_object(context: &mut Context) -> JsObject {
let function_module = function::create_object(context);
let object_module = object::create_object(context);
let shape_module = shape::create_object(context);
Expand Down Expand Up @@ -60,7 +60,7 @@ fn create_boa_object(context: &mut Context<'_>) -> JsObject {
}

#[allow(clippy::redundant_pub_crate)]
pub(crate) fn init_boa_debug_object(context: &mut Context<'_>) {
pub(crate) fn init_boa_debug_object(context: &mut Context) {
let boa_object = create_boa_object(context);
context
.register_global_property(
Expand Down
4 changes: 2 additions & 2 deletions boa_cli/src/debug/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use boa_engine::{
};

/// Returns objects pointer in memory.
fn id(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn id(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let Some(value) = args.get(0) else {
return Err(JsNativeError::typ()
.with_message("expected object argument")
Expand All @@ -21,7 +21,7 @@ fn id(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
Ok(js_string!(format!("0x{:X}", ptr as usize)).into())
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
ObjectInitializer::new(context)
.function(NativeFunction::from_fn_ptr(id), js_string!("id"), 1)
.build()
Expand Down
18 changes: 5 additions & 13 deletions boa_cli/src/debug/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,37 @@ use boa_engine::{
Context, JsArgs, JsObject, JsResult, JsValue, NativeFunction,
};

fn get_constant_folding(
_: &JsValue,
_: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
fn get_constant_folding(_: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
Ok(context
.optimizer_options()
.contains(OptimizerOptions::CONSTANT_FOLDING)
.into())
}

fn set_constant_folding(
_: &JsValue,
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
fn set_constant_folding(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0).to_boolean();
let mut options = context.optimizer_options();
options.set(OptimizerOptions::CONSTANT_FOLDING, value);
context.set_optimizer_options(options);
Ok(JsValue::undefined())
}

fn get_statistics(_: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn get_statistics(_: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
Ok(context
.optimizer_options()
.contains(OptimizerOptions::STATISTICS)
.into())
}

fn set_statistics(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn set_statistics(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let value = args.get_or_undefined(0).to_boolean();
let mut options = context.optimizer_options();
options.set(OptimizerOptions::STATISTICS, value);
context.set_optimizer_options(options);
Ok(JsValue::undefined())
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
let get_constant_folding = FunctionObjectBuilder::new(
context.realm(),
NativeFunction::from_fn_ptr(get_constant_folding),
Expand Down
4 changes: 2 additions & 2 deletions boa_cli/src/debug/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use boa_engine::{
};

/// Creates a new ECMAScript Realm and returns the global object of the realm.
fn create(_: &JsValue, _: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn create(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let context = &mut Context::default();

Ok(context.global_object().into())
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
ObjectInitializer::new(context)
.function(NativeFunction::from_fn_ptr(create), js_string!("create"), 0)
.build()
Expand Down
8 changes: 4 additions & 4 deletions boa_cli/src/debug/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ fn get_object(args: &[JsValue], position: usize) -> JsResult<&JsObject> {
}

/// Returns object's shape pointer in memory.
fn id(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn id(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let object = get_object(args, 0)?;
let object = object.borrow();
let shape = object.shape();
Ok(js_string!(format!("0x{:X}", shape.to_addr_usize())).into())
}

/// Returns object's shape type.
fn r#type(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn r#type(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let object = get_object(args, 0)?;
let object = object.borrow();
let shape = object.shape();
Expand All @@ -41,7 +41,7 @@ fn r#type(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValu
}

/// Returns object's shape type.
fn same(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn same(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let lhs = get_object(args, 0)?;
let rhs = get_object(args, 1)?;

Expand All @@ -60,7 +60,7 @@ fn same(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue>
Ok(JsValue::new(lhs_shape_ptr == rhs_shape_ptr))
}

pub(super) fn create_object(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_object(context: &mut Context) -> JsObject {
ObjectInitializer::new(context)
.function(NativeFunction::from_fn_ptr(id), js_string!("id"), 1)
.function(NativeFunction::from_fn_ptr(r#type), js_string!("type"), 1)
Expand Down
31 changes: 16 additions & 15 deletions boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use boa_engine::{
context::ContextBuilder,
job::{FutureJob, JobQueue, NativeJob},
js_string,
module::{Module, ModuleLoader, SimpleModuleLoader},
module::{Module, SimpleModuleLoader},
optimizer::OptimizerOptions,
property::Attribute,
script::Script,
Expand All @@ -81,7 +81,7 @@ use debug::init_boa_debug_object;
use rustyline::{config::Config, error::ReadlineError, EditMode, Editor};
use std::{
cell::RefCell, collections::VecDeque, eprintln, fs::read, fs::OpenOptions, io, path::PathBuf,
println,
println, rc::Rc,
};

#[cfg(all(
Expand Down Expand Up @@ -233,7 +233,7 @@ enum FlowgraphDirection {
///
/// Returns a error of type String with a error message,
/// if the source has a syntax or parsing error.
fn dump<S>(src: &S, args: &Opt, context: &mut Context<'_>) -> Result<(), String>
fn dump<S>(src: &S, args: &Opt, context: &mut Context) -> Result<(), String>
where
S: AsRef<[u8]> + ?Sized,
{
Expand Down Expand Up @@ -278,7 +278,7 @@ where
}

fn generate_flowgraph(
context: &mut Context<'_>,
context: &mut Context,
src: &[u8],
format: FlowgraphFormat,
direction: Option<FlowgraphDirection>,
Expand All @@ -304,8 +304,8 @@ fn generate_flowgraph(

fn evaluate_files(
args: &Opt,
context: &mut Context<'_>,
loader: &SimpleModuleLoader,
context: &mut Context,
loader: Rc<SimpleModuleLoader>,
) -> Result<(), io::Error> {
for file in &args.files {
let buffer = read(file)?;
Expand Down Expand Up @@ -382,13 +382,14 @@ fn main() -> Result<(), io::Error> {

let args = Opt::parse();

let queue: &dyn JobQueue = &Jobs::default();
let loader = &SimpleModuleLoader::new(&args.root)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
let dyn_loader: &dyn ModuleLoader = loader;
let queue = Rc::new(Jobs::default());
let loader = Rc::new(
SimpleModuleLoader::new(&args.root)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?,
);
let mut context = ContextBuilder::new()
.job_queue(queue)
.module_loader(dyn_loader)
.module_loader(loader.clone())
.build()
.expect("cannot fail with default global object");

Expand Down Expand Up @@ -491,7 +492,7 @@ fn main() -> Result<(), io::Error> {
}

/// Adds the CLI runtime to the context.
fn add_runtime(context: &mut Context<'_>) {
fn add_runtime(context: &mut Context) {
let console = Console::init(context);
context
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
Expand All @@ -502,11 +503,11 @@ fn add_runtime(context: &mut Context<'_>) {
struct Jobs(RefCell<VecDeque<NativeJob>>);

impl JobQueue for Jobs {
fn enqueue_promise_job(&self, job: NativeJob, _: &mut Context<'_>) {
fn enqueue_promise_job(&self, job: NativeJob, _: &mut Context) {
self.0.borrow_mut().push_back(job);
}

fn run_jobs(&self, context: &mut Context<'_>) {
fn run_jobs(&self, context: &mut Context) {
loop {
let jobs = std::mem::take(&mut *self.0.borrow_mut());
if jobs.is_empty() {
Expand All @@ -520,7 +521,7 @@ impl JobQueue for Jobs {
}
}

fn enqueue_future_job(&self, future: FutureJob, _: &mut Context<'_>) {
fn enqueue_future_job(&self, future: FutureJob, _: &mut Context) {
let job = pollster::block_on(future);
self.0.borrow_mut().push_back(job);
}
Expand Down
8 changes: 2 additions & 6 deletions boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ArrayIterator {
pub(crate) fn create_array_iterator(
array: JsObject,
kind: PropertyNameKind,
context: &Context<'_>,
context: &Context,
) -> JsValue {
let array_iterator = JsObject::from_proto_and_data_with_shared_shape(
context.root_shape(),
Expand All @@ -101,11 +101,7 @@ impl ArrayIterator {
/// - [ECMA reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-%arrayiteratorprototype%.next
pub(crate) fn next(
this: &JsValue,
_: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
pub(crate) fn next(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let mut array_iterator = this.as_object().map(JsObject::borrow_mut);
let array_iterator = array_iterator
.as_mut()
Expand Down
Loading
Loading