Skip to content

Instantly share code, notes, and snippets.

@Alhadis
Last active July 15, 2023 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alhadis/f4f431212596653f7b9920fd2c3020d6 to your computer and use it in GitHub Desktop.
Save Alhadis/f4f431212596653f7b9920fd2c3020d6 to your computer and use it in GitHub Desktop.
Guess supported ECMAScript version(s)
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = false
indent_style = tab
[*.{yaml,yml}]
indent_style = space
indent_size = 4
[{COMMIT_EDITMSG,MERGE_MSG}]
trim_trailing_whitespace = true
max_line_length = 72
indent_style = space
indent_size = 4
*.jsx linguist-language=JavaScript
*.html linguist-documentation
.DS_Store
Thumbs.db
coverage
Desktop.ini
node_modules
package-lock.json
yarn.lock
.nyc_output
._*
.\#*
\#*\#
*~
*.log
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
/test.*

js-engine-test.jsx displays the ECMAScript version(s) supported by the JS interpreter. It's designed for older, more obscure implementations of ECMAScript with limited debugging facilities.

Example output

Using JerryScript 2.4.0Using Adobe Illustrator 2023
λ jerry ./js-engine-test.jsx
Engine is limited to...
- ES3:    false
- ES4:    false

Engine supports...
- ES5:    true
- ES6:    true
- ES2016: true
- ES2017: true
- ES2018: true
- ES2019: true
- ES2020: true
- ES2021: false
- ES2022: false
- ES2023: false
Script output in Adobe Illustrator 2023
<!DOCTYPE html>
<html lang="en-AU">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="initial-scale=1, minimum-scale=1"/>
<title>js-engine-test.jsx</title>
</head>
<body>
<script type="text/javascript" src="./js-engine-test.jsx"></script>
</body>
</html>
/**
* @fileoverview Report the ECMAScript version(s) supported by the JavaScript
* engine running this script. Intended for use with ancient or obscure runtimes.
*
* NOTE: This file's `.jsx` extension is needed to be runnable in Adobe Illustrator
* (and presumably other Adobe products that support so-called "ExtendScript" files).
* It has nothing to do with Babel or its overhyped implementation of E4X.
*/
/*@cc_on @if (@_jscript_version > 1 && @_jscript_version <= 11) @*/
if("function" !== typeof alert && (
"function" === typeof print ||
"object" === typeof console && null !== console && "function" === typeof console.log
)){
function alert(value){
if("object" === typeof console)
console.log(value);
else
print(value);
}
}
/*@end @*/
function exec(js){
try{ return eval(js); }
catch(error){}
}
// Supposedly, ES3 engines can also be detected with `"function" === typeof /a/`.
var supportsES3 = (function(){
var failures = 0;
var badWords = [
"abstract",
"boolean",
"byte",
"char",
"double",
"final",
"float",
"goto",
"int",
"long",
"native",
"short",
"synchronized",
"throws",
"transient",
"volatile",
"redHerring"
];
for(var i = 0; i < badWords.length; ++i){
try{ eval("var " + badWords[i] + " = true;"); }
catch(error){ ++failures; }
}
return failures === badWords.length - 1;
})();
var supportsES4 = false; // TODO: Find some way to run this script as ActionScript 3, lol
var supportsES5 = exec("1 === ({ if: 1 }).if;");
var supportsES6 = "function" === typeof (exec("let Foo = () => {}; Foo"));
var supportsES2016 = 16 === exec("4 ** 2");
var supportsES2017 = ("function" === typeof Promise) && exec("(async function(){ await Promise.resolve(true); })()") instanceof Promise;
var supportsES2018 = exec("var X = {b: 2, c: 3}; var O = {a: 1, ...X}; O !== X && 6 === (O.a + O.b + O.c);");
var supportsES2019 = exec("(function(){ try { throw new Error(); } catch { return true; } return false; })()");
var supportsES2020 = 16 === exec("Number(null ?? 16n)");
var supportsES2021 = exec("let a = 1; let i = 1; a ||= ++i; a === 1 && i === 1");
var supportsES2022 = exec("class A{#x; static check(obj){ return #x in obj; }}; A.check(new A) && !A.check({});");
var supportsES2023 = exec("(function(){ try{ return !eval('#!/usr/bin/env osascript -l JavaScript'); } catch(error){ return false; }})()");
alert(
"Engine is limited to..."
+ "\n- ES3: " + !!supportsES3
+ "\n- ES4: " + !!supportsES4
+ "\n\nEngine supports..."
+ "\n- ES5: " + !!supportsES5
+ "\n- ES6: " + !!supportsES6
+ "\n- ES2016: " + !!supportsES2016
+ "\n- ES2017: " + !!supportsES2017
+ "\n- ES2018: " + !!supportsES2018
+ "\n- ES2019: " + !!supportsES2019
+ "\n- ES2020: " + !!supportsES2020
+ "\n- ES2021: " + !!supportsES2021
+ "\n- ES2022: " + !!supportsES2022
+ "\n- ES2023: " + !!supportsES2023
);

Copyright (c) 2023, John Gardner

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#!/usr/bin/env bash
set -e
output=\
'Engine is limited to...
- ES3: false
- ES4: false
Engine supports...
- ES5: true
- ES6: true
- ES2016: true
- ES2017: true
- ES2018: true
- ES2019: true
- ES2020: true
'
test_cmd(){
set -- "$* ./js-engine-test.jsx"
printf '\033[2m$ %s\033[22m\n' "$1" >&2
printf %s "$output" | grep -v 'ES202[1-3]' | diff -U4 <($1 2>&1 | grep -v 'ES202[1-3]') -
}
test_cmd node
test_cmd qjs --script
test_cmd osascript -l JavaScript
test_cmd spidermonkey
test_cmd javascriptcore
test_cmd jerry
test_cmd d8
gjs ./js-engine-test.jsx 2>& 1>/dev/null |
grep -qE '^Gjs-Console-Message: [0-9]{2}:[0-9]{2}:[0-9]{2}\.\d{3}: Engine is limited to\.\.\.$'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment