Skip to content

Commit

Permalink
Merge pull request #1260 from svaarala/add-global-binding
Browse files Browse the repository at this point in the history
Add a 'global' binding
  • Loading branch information
svaarala authored Jan 3, 2017
2 parents 8816d05 + f2702de commit 5fe92c5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions RELEASES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,11 @@ Planned
2.1.0 (XXXX-XX-XX)
------------------

* Add a "global" property to the global object to provide easy access to the
global object itself without needing idioms like
"new Function('return this')()"; implemented based on
https://github.com/tc39/proposal-global (GH-1259, GH-1260)

* Spawn the ArrayBuffer object backing a typed array lazily when its .buffer
property is first read, reducing memory usage in common cases where the view
is constructed directly without needing the ArrayBuffer object (GH-1225)
Expand Down
15 changes: 15 additions & 0 deletions polyfills/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Duktape 2.1.0 adds a 'global' binding. Polyfill for earlier versions.
*/

if (typeof global === 'undefined') {
(function () {
var global = new Function('return this;')();
Object.defineProperty(global, 'global', {
value: global,
writable: true,
enumerable: false,
configurable: true
});
})();
}
10 changes: 10 additions & 0 deletions src-input/builtins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ objects:
bidx: true

properties:
# 'global' binding giving easy to access to the global object.
# Not yet standard, see https://github.com/tc39/proposal-global.
- key: "global"
value:
type: object
id: bi_global
attributes: "wc"
# This could be stripped when DUK_USE_GLOBAL_BUILTIN is disabled
# but keep for now (the property is quite fundamental).

- key: "NaN"
value:
type: double
Expand Down
40 changes: 40 additions & 0 deletions tests/ecmascript/test-bi-global-global-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 'global' binding based on https://github.com/tc39/proposal-global
*/

/*===
object
true
object
true
true
false
true
===*/

function test() {
var pd, g;

// Standard trick to access global object.
g = new Function('return this')();

// proposal-global provides 'global' for cleaner standard access.
print(typeof global);
print(g === global);

// Attributes in current proposal.
pd = Object.getOwnPropertyDescriptor(g, 'global');
print(typeof pd);
if (pd) {
print(pd.value === g);
print(pd.writable);
print(pd.enumerable);
print(pd.configurable);
}
}

try {
test();
} catch (e) {
print(e.stack || e);
}
1 change: 1 addition & 0 deletions util/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ def main():

copy_files([
'console-minimal.js',
'global.js',
'object-prototype-definegetter.js',
'object-prototype-definesetter.js',
'object-assign.js',
Expand Down
5 changes: 5 additions & 0 deletions website/guide/duktapebuiltins.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ <h2>Additional global object properties</h2>
</thead>
<tbody>
<tr>
<td class="propname">global</td>
<td>References the global object itself.
See <a href="https://github.com/tc39/proposal-global">proposal-global</a>.</td>
</tr>
<tr>
<td class="propname"><a href="#builtin-duktape">Duktape</a></td>
<td>The Duktape built-in object. Contains miscellaneous implementation specific stuff.</td>
</tr>
Expand Down

0 comments on commit 5fe92c5

Please sign in to comment.