Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalThis shim #16054

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,13 @@ proc genHeader(): Rope =
var framePtr = null;
var excHandler = 0;
var lastJSError = null;
var globalThis;
if(typeof globalThis == 'undefined') globalThis = (function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this not be true? You start with var globalThis;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var a; typeof a == "undefined";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, you just wrote if (true). I think in order to emulate globalThis you must not predeclare it at all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a= 1; var a; typeof a == "undefined"; it's not like you think.

Copy link
Collaborator Author

@bung87 bung87 Nov 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare it here just note it as global variable, this line can be safely delete.

if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
})();
if (typeof Int8Array === 'undefined') Int8Array = Array;
if (typeof Int16Array === 'undefined') Int16Array = Array;
if (typeof Int32Array === 'undefined') Int32Array = Array;
Expand Down
2 changes: 1 addition & 1 deletion tests/js/tjshello.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
discard """
cmd: "nim $target $options --stackTrace:off --lineTrace:off $file"
output: "Hello World"
maxcodesize: 1000
maxcodesize: 1134
ccodecheck: "!@'function'"
"""

Expand Down