Skip to content

Commit

Permalink
WIP - Update JSON Global (#129)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DomParfitt authored and jasonwilliams committed Oct 11, 2019
1 parent 44d92c9 commit 6c3519f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 11 additions & 9 deletions src/lib/js/json.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 changes: 1 addition & 1 deletion src/lib/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 6c3519f

Please sign in to comment.