11defmodule ElixirScript.Translator.Spawn do
22 alias ESTree.Tools.Builder , as: JS
33 alias ElixirScript.Translator
4- alias ElixirScript.Function
5-
4+ alias ElixirScript.Translator. Function
5+ alias ElixirScript.Translator.Call
66
77 def make_spawn ( func , env ) do
88 do_spawn_with_fn ( func , env , "spawn" )
@@ -12,7 +12,8 @@ defmodule ElixirScript.Translator.Spawn do
1212 do_spawn_with_fn ( func , env , "spawn_link" )
1313 end
1414
15- defp do_spawn_with_fn ( func , env , spawn_func_name ) do
15+ defp do_spawn_with_fn ( { :fn , _ , [ { :-> , _ , [ [ ] , body ] } ] } , env , spawn_func_name ) do
16+ { body , env } = Function . prepare_function_body ( body , % { env | in_process: true } )
1617
1718 js_ast = JS . call_expression (
1819 JS . member_expression (
@@ -24,24 +25,25 @@ defmodule ElixirScript.Translator.Spawn do
2425 )
2526 ) ,
2627 JS . identifier ( "get_global" )
27- ) , [ ] )
28+ ) ,
29+ [ ]
30+ )
2831
2932
3033 js_ast = JS . call_expression (
3134 JS . member_expression (
32- js_ast ,
33- JS . identifier ( "processes" )
35+ JS . member_expression (
36+ js_ast ,
37+ JS . identifier ( "processes" )
38+ ) ,
39+ JS . identifier ( spawn_func_name )
3440 ) ,
35- JS . identifier ( spawn_func_name ) ,
3641 [
37- ! Translator . translate ( func , % { env | in_process: true } )
42+ JS . function_expression ( [ ] , [ ] , JS . block_statement ( body ) , true )
3843 ]
3944 )
4045
41-
4246 { js_ast , env }
43-
44-
4547 end
4648
4749 def make_spawn ( module , fun , args , env ) do
@@ -78,34 +80,30 @@ defmodule ElixirScript.Translator.Spawn do
7880 JS . identifier ( "run" )
7981 )
8082
81-
82- module_name = Function . get_module_name_for_function ( module , env )
83- func = to_string ( fun )
84- args = Translator . translate! ( args , env )
83+ { js , _ } = Call . make_function_call ( Translator . create_module_name ( module , env ) , fun , args , env )
84+ % ESTree.CallExpression { callee: % ESTree.MemberExpression { object: module , property: % ESTree.Identifier { name: fun } } , arguments: args } = js
8585
8686 func_to_run = JS . member_expression (
87- JS . identifier ( module_name ) ,
88- JS . identifier ( func ) ,
87+ module ,
88+ JS . literal ( fun ) ,
8989 true
9090 )
9191
92-
9392 js_ast = JS . call_expression (
9493 JS . member_expression (
95- js_ast ,
96- JS . identifier ( "processes" )
94+ JS . member_expression (
95+ js_ast ,
96+ JS . identifier ( "processes" )
97+ ) ,
98+ JS . identifier ( spawn_func_name )
9799 ) ,
98- JS . identifier ( spawn_func_name ) ,
99100 [
100101 run_func ,
101- JS . array_expression ( [ func_to_run , args ] )
102+ JS . array_expression ( [ func_to_run , JS . array_expression ( args ) ] )
102103 ]
103104 )
104105
105-
106106 { js_ast , env }
107-
108-
109107 end
110108
111109
0 commit comments