Browse Source

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
pull/147/head
Dom Parfitt 5 years ago committed by Jason Williams
parent
commit
6c3519f957
  1. 5
      CHANGELOG.md
  2. 20
      src/lib/js/json.rs
  3. 2
      src/lib/realm.rs

5
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.

20
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
/// <https://tc39.github.io/ecma262/#sec-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
}

2
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));
}

Loading…
Cancel
Save