Skip to content

Commit

Permalink
Add globalThis property to global object.
Browse files Browse the repository at this point in the history
Summary:
`globalThis` is a TC39 proposal which has gained adoption in various
JS implementations.

Add the property to the global object.

Reviewed By: dulinriley

Differential Revision: D16284456

fbshipit-source-id: b02403223c677e2d0e310606ab17374746b694ea
  • Loading branch information
avp authored and facebook-github-bot committed Jul 16, 2019
1 parent c197ba0 commit d088f6f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions include/hermes/BCGen/HBC/BytecodeFileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const static uint64_t MAGIC = 0x1F1903C103BC1FC6;
const static uint64_t DELTA_MAGIC = ~MAGIC;

// Bytecode version generated by this version of the compiler.
// Updated: Jun 22, 2019
const static uint32_t BYTECODE_VERSION = 59;
// Updated: Jul 16, 2019
const static uint32_t BYTECODE_VERSION = 60;

/// Property cache index which indicates no caching.
static constexpr uint8_t PROPERTY_CACHING_DISABLED = 0;
Expand Down
1 change: 1 addition & 0 deletions include/hermes/VM/PredefinedStrings.def
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ STR(__lookupGetter__, "__lookupGetter__")
STR(__lookupSetter__, "__lookupSetter__")
STR(assign, "assign")
STR(raw, "raw")
STR(globalThis, "globalThis")

STR(String, "String")
STR(fromCharCode, "fromCharCode")
Expand Down
8 changes: 8 additions & 0 deletions lib/VM/JSLib/GlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,14 @@ void initGlobalObject(Runtime *runtime) {
encodeURIComponent,
1);

// Define the 'globalThis' property.
runtime->ignoreAllocationFailure(JSObject::defineOwnProperty(
runtime->getGlobal(),
runtime,
Predefined::getSymbolID(Predefined::globalThis),
normalDPF,
runtime->getGlobal()));

// Define the 'require' function.
runtime->requireFunction =
NativeFunction::create(
Expand Down
13 changes: 13 additions & 0 deletions test/hermes/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
//
// RUN: %hermes -non-strict -target=HBC %s | %FileCheck --match-full-lines %s
// Make sure the global object has a prototype and prints as the correct class.
// Also check that it can be accessed via globalThis.

print(this);
//CHECK: [object global]

print(globalThis);
//CHECK: [object global]

(function() {
print(globalThis);
//CHECK: [object global]
})();

var desc = Object.getOwnPropertyDescriptor(globalThis, 'globalThis');
print(desc.writable, desc.enumerable, desc.configurable);
//CHECK: true false true
2 changes: 1 addition & 1 deletion tools/hbc-attribute/hbc-attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ using SLG = hermes::hbc::SerializedLiteralGenerator;
* If you have added or modified sections, make sure they're counted properly.
*/
static_assert(
BYTECODE_VERSION == 59,
BYTECODE_VERSION == 60,
"Bytecode version changed. Please verify that hbc-attribute counts correctly..");

static llvm::cl::opt<std::string> InputFilename(
Expand Down
1 change: 1 addition & 0 deletions unittests/VMRuntime/PredefinedStrings.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ EXPECT("__lookupGetter__")
EXPECT("__lookupSetter__")
EXPECT("assign")
EXPECT("raw")
EXPECT("globalThis")

EXPECT("String")
EXPECT("fromCharCode")
Expand Down

0 comments on commit d088f6f

Please sign in to comment.