Skip to content
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
16 changes: 8 additions & 8 deletions crates/yavashark_bytecode_interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ impl ByteCodeInterpreter {
if func.is_generator && !func.is_async {
let g = GeneratorFunction::new(compiled.unwrap_or_default(), scope, realm, params);

g.define_variable("length".into(), len.into())?;
g.define_variable("name".into(), name.into())?;
g.define_property_attributes("length".into(), len.into(), realm)?;
g.define_property_attributes("name".into(), name.into(), realm)?;

return Ok(g.into_object());
}

if func.is_generator && func.is_async {
let g = AsyncGeneratorFunction::new(compiled.unwrap_or_default(), scope, realm, params);

g.define_variable("length".into(), len.into())?;
g.define_variable("name".into(), name.into())?;
g.define_property_attributes("length".into(), len.into(), realm)?;
g.define_property_attributes("name".into(), name.into(), realm)?;

return Ok(g.into_object());
}
Expand All @@ -100,16 +100,16 @@ impl ByteCodeInterpreter {
if func.is_async {
let f = AsyncBytecodeFunction::new(compiled.unwrap_or_default(), scope, realm, params);

f.define_variable("length".into(), len.into())?;
f.define_variable("name".into(), name.into())?;
f.define_property_attributes("length".into(), len.into(), realm)?;
f.define_property_attributes("name".into(), name.into(), realm)?;

return Ok(f.into_object());
}

let f = BytecodeFunction::new(compiled.unwrap_or_default(), scope, realm, params);

f.define_variable("length".into(), len.into())?;
f.define_variable("name".into(), name.into())?;
f.define_property_attributes("length".into(), len.into(), realm)?;
f.define_property_attributes("name".into(), name.into(), realm)?;

Ok(f.into_object())
}
Expand Down
85 changes: 42 additions & 43 deletions crates/yavashark_env/src/builtins/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::array::{ArrayIterator, MutableArrayIterator};
use crate::error::Error;
use crate::value::{MutObj, Obj, ObjectImpl};
use crate::{MutObject, ObjectProperty, Realm, Res, Value, ValueResult, Variable};
use crate::value::{DefinePropertyResult, MutObj, Obj, ObjectImpl, Property};
use crate::{InternalPropertyKey, MutObject, Realm, Res, Value, ValueResult, Variable};
use std::cell::{Cell, RefCell};
use std::ops::{Deref, DerefMut};
use yavashark_macro::props;
use yavashark_string::YSString;

#[derive(Debug)]
pub struct Arguments {
Expand All @@ -26,8 +25,8 @@ impl Arguments {
}
}

pub fn resolve_array(&self, idx: usize) -> Option<ObjectProperty> {
Some(self.args.borrow().get(idx)?.copy().into())
pub fn resolve_array(&self, idx: usize) -> Option<Value> {
Some(self.args.borrow().get(idx)?.copy())
}

pub fn set_array(&self, idx: usize, value: Value) -> Res<()> {
Expand All @@ -54,69 +53,69 @@ impl ObjectImpl for Arguments {
self.inner.borrow_mut()
}

fn define_property(&self, name: Value, value: Value) -> Res<()> {
if let Value::Number(idx) = &name {
if let Some(v) = self.args.borrow_mut().get_mut(*idx as usize) {
fn define_property(&self, name: InternalPropertyKey, value: Value, realm: &mut Realm) -> Res<DefinePropertyResult> {
if let InternalPropertyKey::Index(idx) = name {
if let Some(v) = self.args.borrow_mut().get_mut(idx) {
*v = value;
return Ok(());
return Ok(DefinePropertyResult::Handled);
}
}

if let Value::String(s) = &name {
if let InternalPropertyKey::String(s) = &name {
if s == "length" {
*self.length.borrow_mut() = value;
return Ok(());
return Ok(DefinePropertyResult::Handled);
}
}

self.get_wrapped_object().define_property(name, value)
self.get_wrapped_object().define_property(name, value, realm)
}

fn define_variable(&self, name: Value, value: Variable) -> Res<()> {
if let Value::Number(idx) = &name {
if let Some(v) = self.args.borrow_mut().get_mut(*idx as usize) {
fn define_property_attributes(&self, name: InternalPropertyKey, value: Variable, realm: &mut Realm) -> Res<DefinePropertyResult> {
if let InternalPropertyKey::Index(idx) = name {
if let Some(v) = self.args.borrow_mut().get_mut(idx) {
*v = value.value;
return Ok(());
return Ok(DefinePropertyResult::Handled);
}
}

if let Value::String(s) = &name {
if let InternalPropertyKey::String(s) = &name {
if s == "length" {
*self.length.borrow_mut() = value.value;
return Ok(());
return Ok(DefinePropertyResult::Handled);
}
}

self.get_wrapped_object().define_variable(name, value)
self.get_wrapped_object().define_property_attributes(name, value, realm)
}

fn resolve_property(&self, name: &Value) -> Res<Option<ObjectProperty>> {
if let Value::Number(idx) = &name {
if let Some(value) = self.resolve_array(*idx as usize) {
return Ok(Some(value));
fn resolve_property(&self, name: InternalPropertyKey, realm: &mut Realm) -> Res<Option<Property>> {
if let InternalPropertyKey::Index(idx) = name {
if let Some(value) = self.resolve_array(idx) {
return Ok(Some(Property::Value(value.into())));
}
}

if let Value::String(s) = &name {
if let InternalPropertyKey::String(s) = &name {
if s == "length" {
return Ok(Some(self.length.borrow().clone().into()));
return Ok(Some(Property::Value(self.length.borrow().clone().into())));
}
if s == "callee" {
return Ok(Some(self.callee.clone().into()));
return Ok(Some(Property::Value(self.callee.clone().into())));
}
}

self.get_wrapped_object().resolve_property(name)
self.get_wrapped_object().resolve_property(name, realm)
}

fn get_property(&self, name: &Value) -> Res<Option<ObjectProperty>> {
if let Value::Number(idx) = &name {
if let Some(value) = self.resolve_array(*idx as usize) {
return Ok(Some(value));
fn get_own_property(&self, name: InternalPropertyKey, realm: &mut Realm) -> Res<Option<Property>> {
if let InternalPropertyKey::Index(idx) = name {
if let Some(value) = self.resolve_array(idx) {
return Ok(Some(value.into()));
}
}

if let Value::String(s) = &name {
if let InternalPropertyKey::String(s) = &name {
if s == "length" {
return Ok(Some(self.length.borrow().clone().into()));
}
Expand All @@ -125,22 +124,22 @@ impl ObjectImpl for Arguments {
}
}

self.get_wrapped_object().get_property(name)
self.get_wrapped_object().get_own_property(name, realm)
}

fn name(&self) -> String {
"Arguments".to_string()
}

fn to_string(&self, _: &mut Realm) -> Result<YSString, Error> {
Ok("[object Arguments]".into())
}

fn to_string_internal(&self) -> Result<YSString, Error> {
Ok("[object Arguments]".into())
}

fn get_array_or_done(&self, index: usize) -> Result<(bool, Option<Value>), Error> {
//
// fn to_string(&self, _: &mut Realm) -> Result<YSString, Error> {
// Ok("[object Arguments]".into())
// }
//
// fn to_string_internal(&self) -> Result<YSString, Error> {
// Ok("[object Arguments]".into())
// }

fn get_array_or_done(&self, index: usize, _: &mut Realm) -> Result<(bool, Option<Value>), Error> {
let args = self.args.borrow();
if index < args.len() {
Ok((false, Some(args[index].clone())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct BigInt64Array {}

impl BigInt64Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.bigint64array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.bigint64array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableBigInt64Array {}),
Expand All @@ -37,3 +37,5 @@ impl BigInt64Array {
Ok(Self::new(realm, ty)?.into_object())
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct BigUint64Array {}

impl BigUint64Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.biguint64array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.biguint64array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableBigUint64Array {}),
Expand Down
6 changes: 3 additions & 3 deletions crates/yavashark_env/src/builtins/arraybuffer/dataview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub struct DataViewConstructor {}

impl DataViewConstructor {
#[allow(clippy::new_ret_no_self)]
pub fn new(_: &Object, func: ObjectHandle) -> crate::Res<ObjectHandle> {
pub fn new(_: &Object, func: ObjectHandle, _realm: &mut Realm) -> crate::Res<ObjectHandle> {
let this = Self {
inner: RefCell::new(MutableDataViewConstructor {
object: MutObject::with_proto(func),
Expand All @@ -357,7 +357,7 @@ impl DataViewConstructor {
}

impl Constructor for DataViewConstructor {
fn construct(&self, realm: &mut Realm, args: Vec<Value>) -> ValueResult {
fn construct(&self, realm: &mut Realm, args: Vec<Value>) -> Res<ObjectHandle> {
let buffer = args
.first()
.map_or(Err(Error::ty("DataView requires a buffer argument")), |v| {
Expand All @@ -376,6 +376,6 @@ impl Constructor for DataViewConstructor {
None => None,
};

Ok(DataView::new(realm, buffer, byte_offset, byte_length)?.into_value())
Ok(DataView::new(realm, buffer, byte_offset, byte_length)?.into_object())
}
}
4 changes: 2 additions & 2 deletions crates/yavashark_env/src/builtins/arraybuffer/float16array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use yavashark_macro::{object, props};
pub struct Float16Array {}

impl Float16Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.float16array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.float16array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableFloat16Array {}),
Expand Down
4 changes: 2 additions & 2 deletions crates/yavashark_env/src/builtins/arraybuffer/float32array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct Float32Array {}

impl Float32Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.float32array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.float32array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableFloat32Array {}),
Expand Down
4 changes: 2 additions & 2 deletions crates/yavashark_env/src/builtins/arraybuffer/float64array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct Float64Array {}

impl Float64Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.float64array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.float64array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableFloat64Array {}),
Expand Down
6 changes: 4 additions & 2 deletions crates/yavashark_env/src/builtins/arraybuffer/int16array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct Int16Array {}

impl Int16Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.int16array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.int16array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableInt16Array {}),
Expand All @@ -37,3 +37,5 @@ impl Int16Array {
Ok(Self::new(realm, ty)?.into_object())
}
}


4 changes: 2 additions & 2 deletions crates/yavashark_env/src/builtins/arraybuffer/int32array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct Int32Array {}

impl Int32Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.int32array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.int32array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableInt32Array {}),
Expand Down
4 changes: 2 additions & 2 deletions crates/yavashark_env/src/builtins/arraybuffer/int8array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use yavashark_macro::{object, props};
pub struct Int8Array {}

impl Int8Array {
pub fn new(realm: &Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.int8array.clone().into())?;
pub fn new(realm: &mut Realm, ty: TypedArray) -> Res<Self> {
ty.set_prototype(realm.intrinsics.int8array.clone().into(), realm)?;

Ok(Self {
inner: RefCell::new(MutableInt8Array {}),
Expand Down
Loading
Loading