4040 */
4141package com .oracle .truffle .trufflenode ;
4242
43+ import java .io .OutputStream ;
4344import java .net .URL ;
4445import java .net .URLClassLoader ;
4546import java .nio .file .Path ;
4647import java .util .ArrayList ;
47- import java .util .Arrays ;
48- import java .util .HashSet ;
4948import java .util .List ;
5049import java .util .Map ;
5150import java .util .Optional ;
@@ -119,20 +118,22 @@ public static class OptionsParser extends AbstractLanguageLauncher implements Fu
119118 private static final String INSPECT = "inspect" ;
120119 private static final String INSPECT_SUSPEND = "inspect.Suspend" ;
121120 private static final String INSPECT_WAIT_ATTACHED = "inspect.WaitAttached" ;
121+ private static final String WASM_LANGUAGE_ID = "wasm" ;
122122
123123 private Context .Builder contextBuilder ;
124124 private boolean exposeGC ;
125125 private boolean polyglot ;
126126 private boolean unsafeWasmMemory ;
127127 private boolean auxEngineCacheMode ;
128+ private boolean wasmEnabled ;
128129
129- private static final Set <String > AUX_CACHE_OPTIONS = new HashSet <>( Arrays . asList ("engine.Cache" ,
130+ private static final Set <String > AUX_CACHE_OPTIONS = Set . of ("engine.Cache" ,
130131 "engine.CacheLoad" ,
131- "engine.CacheStore" )) ;
132+ "engine.CacheStore" );
132133
133134 // Options that should not be passed to polyglot engine (they are processed
134135 // elsewhere or can be ignored without almost any harm).
135- private static final Set <String > IGNORED_OPTIONS = new HashSet <>( Arrays . asList (new String []{
136+ private static final Set <String > IGNORED_OPTIONS = Set . of (new String []{
136137 "debug-code" ,
137138 "es-staging" ,
138139 "experimental-modules" ,
@@ -158,7 +159,7 @@ public static class OptionsParser extends AbstractLanguageLauncher implements Fu
158159 "nouse-idle-notification" ,
159160 "stack-size" ,
160161 "use-idle-notification"
161- })) ;
162+ });
162163
163164 @ Override
164165 public Object [] apply (String [] args ) {
@@ -189,7 +190,7 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
189190 polyglotOptions .put ("js.string-length-limit" , Integer .toString ((1 << 29 ) - 24 )); // v8::String::kMaxLength
190191
191192 List <String > unprocessedArguments = new ArrayList <>();
192- boolean optWebAssembly = false ;
193+ Boolean optWebAssembly = null ;
193194 Boolean optUnsafeWasmMemory = null ;
194195 for (String arg : arguments ) {
195196 String key = "" ;
@@ -298,10 +299,18 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
298299 }
299300 unprocessedArguments .add (arg );
300301 }
301- if (optWebAssembly ) {
302- if (optUnsafeWasmMemory == null && polyglot && isLanguageAvailable ("wasm" )) {
303- optUnsafeWasmMemory = Boolean .TRUE ;
304- polyglotOptions .put ("wasm.UseUnsafeMemory" , "true" );
302+ if (optWebAssembly != Boolean .FALSE ) {
303+ // WebAssembly is enabled by default, if available.
304+ if (isWasmAvailable ()) {
305+ wasmEnabled = true ;
306+ if (optWebAssembly == null ) {
307+ optWebAssembly = Boolean .TRUE ;
308+ polyglotOptions .put ("js.webassembly" , "true" );
309+ }
310+ if (optWebAssembly && optUnsafeWasmMemory == null ) {
311+ optUnsafeWasmMemory = Boolean .TRUE ;
312+ polyglotOptions .put ("wasm.UseUnsafeMemory" , "true" );
313+ }
305314 }
306315 if (optUnsafeWasmMemory == Boolean .TRUE ) {
307316 unsafeWasmMemory = true ;
@@ -347,6 +356,7 @@ protected void printHelp(OptionCategory maxCategory) {
347356 printOption ("-r, --require" , "module to preload (option can be repeated)" );
348357 printOption ("--inspect[=port]" , "activate inspector on port (overrides options of Chrome Inspector)" );
349358 printOption ("--inspect-brk[=port]" , "activate inspector on port and break at start of user script (overrides options of Chrome Inspector)" );
359+ // @formatter:on
350360 }
351361
352362 private static void printOption (String option , String description ) {
@@ -362,11 +372,26 @@ private static void printOption(String option, String description) {
362372
363373 @ Override
364374 protected String [] getDefaultLanguages () {
365- return polyglot ? new String [0 ] : super .getDefaultLanguages ();
375+ if (polyglot ) {
376+ return new String [0 ];
377+ } else if (wasmEnabled ) {
378+ assert isWasmAvailable () : "wasm not available" ;
379+ return new String []{getLanguageId (), WASM_LANGUAGE_ID };
380+ } else {
381+ return super .getDefaultLanguages ();
382+ }
383+ }
384+
385+ private static boolean isWasmAvailable () {
386+ return isLanguageAvailable (WASM_LANGUAGE_ID );
366387 }
367388
368- static boolean isLanguageAvailable (String languageId ) {
369- try (Engine tempEngine = Engine .newBuilder ().useSystemProperties (false ).build ()) {
389+ private static boolean isLanguageAvailable (String languageId ) {
390+ try (Engine tempEngine = Engine .newBuilder ().useSystemProperties (false ).//
391+ out (OutputStream .nullOutputStream ()).//
392+ err (OutputStream .nullOutputStream ()).//
393+ option ("engine.WarnInterpreterOnly" , "false" ).//
394+ build ()) {
370395 return tempEngine .getLanguages ().containsKey (languageId );
371396 }
372397 }
0 commit comments