@@ -519,39 +519,56 @@ private boolean doctestOutputMatches(String expected, String actual) {
519
519
/**
520
520
* The spawn function runs a given function or script in a different thread.
521
521
*
522
- * <p>js> function g() { a = 7; } js> a = 3; 3 js> spawn(g) Thread[Thread-1,5,main]
523
- * js> a 3
522
+ * <pre>
523
+ * js> function g() { a = 7; }
524
+ * js> a = 3; 3
525
+ * js> spawn(g) Thread[Thread-1,5,main]
526
+ * js> a 7
527
+ * </pre>
524
528
*/
525
529
public static Object spawn (Context cx , Scriptable thisObj , Object [] args , Function funObj ) {
526
530
Scriptable scope = funObj .getParentScope ();
527
- Runner runner ;
531
+ ContextAction <?> action = getAsyncAction (cx , args , scope );
532
+ ContextFactory factory = cx .getFactory ();
533
+ Thread thread = new Thread (() -> factory .call (action ));
534
+ thread .start ();
535
+ return cx .getWrapFactory ().wrap (cx , scope , thread , Thread .class );
536
+ }
537
+
538
+ private static ContextAction <Object > getAsyncAction (
539
+ Context cx , Object [] args , Scriptable scope ) {
540
+ ContextAction <Object > action ;
528
541
if (args .length != 0 && args [0 ] instanceof Function ) {
529
- Object [] newArgs = null ;
530
- if (args .length > 1 && args [1 ] instanceof Scriptable ) {
531
- newArgs = cx .getElements ((Scriptable ) args [1 ]);
532
- }
533
- if (newArgs == null ) {
534
- newArgs = ScriptRuntime .emptyArgs ;
535
- }
536
- runner = new Runner (scope , (Function ) args [0 ], newArgs );
542
+ Function f = (Function ) args [0 ];
543
+ Object [] newArgs =
544
+ args .length > 1 && args [1 ] instanceof Scriptable
545
+ ? cx .getElements ((Scriptable ) args [1 ])
546
+ : ScriptRuntime .emptyArgs ;
547
+ action = cx2 -> f .call (cx2 , scope , scope , newArgs );
537
548
} else if (args .length != 0 && args [0 ] instanceof Script ) {
538
- runner = new Runner (scope , (Script ) args [0 ]);
549
+ Script s = (Script ) args [0 ];
550
+ action = cx2 -> s .exec (cx2 , scope , scope );
539
551
} else {
540
552
throw reportRuntimeError ("msg.spawn.args" );
541
553
}
542
- runner .factory = cx .getFactory ();
543
- Thread thread = new Thread (runner );
544
- thread .start ();
545
- return thread ;
554
+ return action ;
546
555
}
547
556
548
557
/**
549
558
* The sync function creates a synchronized function (in the sense of a Java synchronized
550
559
* method) from an existing function. The new function synchronizes on the the second argument
551
- * if it is defined, or otherwise the <code>this</code> object of its invocation. js> var o =
552
- * { f : sync(function(x) { print("entry"); Packages.java.lang.Thread.sleep(x*1000);
553
- * print("exit"); })}; js> spawn(function() {o.f(5);}); Thread[Thread-0,5,main] entry js>
554
- * spawn(function() {o.f(5);}); Thread[Thread-1,5,main] js> exit entry exit
560
+ * if it is defined, or otherwise the <code>this</code> object of its invocation.
561
+ *
562
+ * <pre>
563
+ * js> var o = { f : sync(function(x) {
564
+ * print("entry");
565
+ * Packages.java.lang.Thread.sleep(x*1000);
566
+ * print("exit");
567
+ * })};
568
+ * js> spawn(function() {o.f(5);}); Thread[Thread-0,5,main] entry
569
+ * js> spawn(function() {o.f(5);}); Thread[Thread-1,5,main]
570
+ * js> exit entry exit
571
+ * </pre>
555
572
*/
556
573
public static Object sync (Context cx , Scriptable thisObj , Object [] args , Function funObj ) {
557
574
if (args .length >= 1 && args .length <= 2 && args [0 ] instanceof Function ) {
@@ -885,34 +902,3 @@ public interface CommandExecutor {
885
902
Process exec (String [] cmdarray , String [] envp , File dir ) throws IOException ;
886
903
}
887
904
}
888
-
889
- class Runner implements Runnable , ContextAction <Object > {
890
-
891
- Runner (Scriptable scope , Function func , Object [] args ) {
892
- this .scope = scope ;
893
- f = func ;
894
- this .args = args ;
895
- }
896
-
897
- Runner (Scriptable scope , Script script ) {
898
- this .scope = scope ;
899
- s = script ;
900
- }
901
-
902
- @ Override
903
- public void run () {
904
- factory .call (this );
905
- }
906
-
907
- @ Override
908
- public Object run (Context cx ) {
909
- if (f != null ) return f .call (cx , scope , scope , args );
910
- else return s .exec (cx , scope , scope );
911
- }
912
-
913
- ContextFactory factory ;
914
- private Scriptable scope ;
915
- private Function f ;
916
- private Script s ;
917
- private Object [] args ;
918
- }
0 commit comments