From 6c3519f95794d12a3078e9a074c8d6376fc46b42 Mon Sep 17 00:00:00 2001 From: Dom Parfitt <11363907+DomParfitt@users.noreply.github.com> Date: Fri, 11 Oct 2019 15:21:25 +0100 Subject: [PATCH] WIP - Update JSON Global (#129) * Added create_constructor method * Updated global to use json::create_constructor method * unassigned var is undefined (#125) * Unassigned variables are set to `undefined` not `null` Fixes #113 * Rust tests for `var x` and `let x` default to undefined * CHANGELOG for issue #113 fix + add tests/js/test.js to gitignore. * fixing PR benchmarks (#132) * fixing PR benchmarks * Push after rebase * Updated import order * Formatting --- CHANGELOG.md | 5 +++++ src/lib/js/json.rs | 20 +++++++++++--------- src/lib/realm.rs | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2823afb32d..8f1f6e0d7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,11 @@ Bug fixes: Unassigned variables have default of undefined (@pop) - Tidy up Globals being added to Global Object. Thanks @DomParfitt +Bug fixes: + +- [BUG #113](https://github.com/jasonwilliams/boa/issues/113): + Unassigned variables have default of undefined (@pop) + # 0.4.0 (2019-09-25) v0.4.0 brings quite a big release. The biggest feature to land is the support of regular expressions. diff --git a/src/lib/js/json.rs b/src/lib/js/json.rs index 7f1bcf7f2d..a44a05ee7b 100644 --- a/src/lib/js/json.rs +++ b/src/lib/js/json.rs @@ -1,5 +1,6 @@ use crate::exec::Interpreter; use crate::js::function::NativeFunctionData; +use crate::js::object::{Object, ObjectKind, PROTOTYPE}; /// The JSON Object /// use crate::js::value::{to_value, ResultValue, Value, ValueData}; @@ -27,14 +28,15 @@ pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue } /// Create a new `JSON` object -pub fn _create(global: &Value) -> Value { - let object = ValueData::new_obj(Some(global)); - object.set_field_slice("stringify", to_value(stringify as NativeFunctionData)); - object.set_field_slice("parse", to_value(parse as NativeFunctionData)); - object -} +pub fn create_constructor(global: &Value) -> Value { + let mut json = Object::default(); + json.kind = ObjectKind::Ordinary; + + let prototype = ValueData::new_obj(Some(global)); + prototype.set_field_slice("parse", to_value(parse as NativeFunctionData)); + prototype.set_field_slice("stringify", to_value(stringify as NativeFunctionData)); -/// Initialise the global object with the `JSON` object -pub fn init(global: &Value) { - global.set_field_slice("JSON", _create(global)); + let json_value = to_value(json); + json_value.set_field_slice(PROTOTYPE, prototype); + json_value } diff --git a/src/lib/realm.rs b/src/lib/realm.rs index 9aa75babe6..76eb52eaff 100644 --- a/src/lib/realm.rs +++ b/src/lib/realm.rs @@ -54,12 +54,12 @@ impl Realm { // Create intrinsics, add global objects here object::init(global); function::init(global); - json::init(global); global.set_field_slice("String", string::create_constructor(global)); global.set_field_slice("RegExp", regexp::create_constructor(global)); global.set_field_slice("Array", array::create_constructor(global)); global.set_field_slice("Boolean", boolean::create_constructor(global)); + global.set_field_slice("JSON", json::create_constructor(global)); global.set_field_slice("Math", math::create_constructor(global)); global.set_field_slice("console", console::create_constructor(global)); }