@@ -4,6 +4,7 @@ use crate::esscript::EsScript;
4
4
use crate :: quickjsruntime:: QuickJsRuntime ;
5
5
use quick_js:: { ExecutionError , JsValue } ;
6
6
use std:: sync:: Arc ;
7
+ use log:: error;
7
8
8
9
pub struct EsRuntime {
9
10
event_queue : Arc < EsEventQueue > ,
@@ -38,9 +39,19 @@ impl EsRuntime {
38
39
39
40
pub fn gc_sync ( ) { }
40
41
41
- pub fn load_module ( ) { }
42
+ pub fn eval_module ( & self , script : EsScript ) {
43
+ self . add_to_event_queue ( |qjs_rt| {
44
+ let res = qjs_rt. eval_module ( script) ;
45
+ match res {
46
+ Ok ( _) => { }
47
+ Err ( e) => log:: error!( "error in async eval {}" , e) ,
48
+ }
49
+ } ) ;
50
+ }
42
51
43
- pub fn load_module_sync ( ) { }
52
+ pub fn eval_module_sync ( & self , script : EsScript ) -> Result < JsValue , ExecutionError > {
53
+ self . add_to_event_queue_sync ( |qjs_rt| qjs_rt. eval_module ( script) )
54
+ }
44
55
45
56
pub fn new_class_builder ( ) { }
46
57
@@ -50,27 +61,49 @@ impl EsRuntime {
50
61
{
51
62
self . event_queue
52
63
. add_task ( || QuickJsRuntime :: do_with ( consumer) ) ;
64
+ self . _add_job_run_task ( ) ;
53
65
}
54
66
55
67
pub fn add_to_event_queue_sync < C , R > ( & self , consumer : C ) -> R
56
68
where
57
69
C : FnOnce ( & QuickJsRuntime ) -> R + Send + ' static ,
58
70
R : Send + ' static ,
59
71
{
60
- self . event_queue
61
- . exe_task ( || QuickJsRuntime :: do_with ( consumer) )
72
+ let res = self . event_queue
73
+ . exe_task ( || QuickJsRuntime :: do_with ( consumer) ) ;
74
+ self . _add_job_run_task ( ) ;
75
+ res
62
76
}
63
77
64
78
pub fn add_helper_task ( ) { }
79
+
80
+ fn _add_job_run_task ( & self ) {
81
+ self . event_queue . add_task ( || {
82
+ QuickJsRuntime :: do_with ( |quick_js_rt| {
83
+ while quick_js_rt. has_pending_jobs ( ) {
84
+
85
+ let res = quick_js_rt. run_pending_job ( ) ;
86
+ match res {
87
+ Ok ( _) => { } ,
88
+ Err ( e) => {
89
+ error ! ( "job run failed: {}" , e) ;
90
+ } ,
91
+ }
92
+
93
+ }
94
+ } )
95
+ } ) ;
96
+ }
65
97
}
66
98
67
99
#[ cfg( test) ]
68
100
pub mod tests {
69
101
use crate :: esruntime:: EsRuntime ;
70
102
use crate :: esscript:: EsScript ;
71
103
use log:: LevelFilter ;
72
- use quick_js:: JsValue ;
104
+ use quick_js:: { JsValue , ExecutionError } ;
73
105
use std:: sync:: Arc ;
106
+ use :: log:: debug;
74
107
75
108
lazy_static ! {
76
109
pub static ref TEST_ESRT : Arc <EsRuntime > = init( ) ;
@@ -83,13 +116,17 @@ pub mod tests {
83
116
EsRuntime :: builder ( ) . build ( )
84
117
}
85
118
119
+
120
+
86
121
#[ test]
87
122
fn test_eval_sync ( ) {
88
123
let rt = & TEST_ESRT ;
89
- rt. eval ( EsScript :: new (
124
+ rt. eval_sync ( EsScript :: new (
90
125
"test.es" . to_string ( ) ,
91
126
"console.log('foo bar');" . to_string ( ) ,
92
- ) ) ;
127
+ ) ) . ok ( ) . expect ( "eval script failed" ) ;
128
+
129
+
93
130
94
131
let res = rt
95
132
. eval_sync ( EsScript :: new ( "test.es" . to_string ( ) , "(2 * 7);" . to_string ( ) ) )
@@ -98,4 +135,50 @@ pub mod tests {
98
135
99
136
assert_eq ! ( res, JsValue :: Int ( 14 ) ) ;
100
137
}
138
+
139
+ #[ test]
140
+ fn test_promise ( ) {
141
+ let rt = & TEST_ESRT ;
142
+
143
+ rt. eval_sync ( EsScript :: new (
144
+ "testp2.es" . to_string ( ) ,
145
+ "let r = {a: 1};console.log('setting up prom');let p = new Promise((res, rej) => {console.log('before res');res(123);console.log('after res');return 456;}).then((a) => {r.a = 2;console.log('prom ressed to ' + a);}).catch((x) => {console.log('p.ca ex=' + x);});" . to_string ( ) ,
146
+ ) ) . ok ( ) . expect ( "eval script failed" ) ;
147
+
148
+ rt. eval_sync ( EsScript :: new (
149
+ "testp2.es" . to_string ( ) ,
150
+ "console.log('r.a = ' + r.a + ' p= ' + p);" . to_string ( ) ,
151
+ ) ) . ok ( ) . expect ( "eval script failed" ) ;
152
+ }
153
+
154
+ #[ test]
155
+ fn test_module_sync ( ) {
156
+ let rt = & TEST_ESRT ;
157
+ debug ! ( "test static import" ) ;
158
+ let res: Result < JsValue , ExecutionError > = rt. eval_module_sync ( EsScript :: new (
159
+ "test.es" . to_string ( ) ,
160
+ "import {some} from 'test_module.mes';\n console.log(some.foo);" . to_string ( ) ,
161
+ ) ) ;
162
+
163
+ match res {
164
+ Ok ( _) => { } ,
165
+ Err ( e) => {
166
+ log:: error!( "static import failed: {}" , e) ;
167
+ } ,
168
+ }
169
+
170
+ debug ! ( "test dynamic import" ) ;
171
+ let res: Result < JsValue , ExecutionError > = rt. eval_module_sync ( EsScript :: new (
172
+ "test.es" . to_string ( ) ,
173
+ "console.log('about to load dynamic module');import('test_module.mes').then((some) => {console.log('after dyn ' + some);console.log(some.mltpl(1, 2));}).catch((x) => {console.log('imp.cat x=' + x);});" . to_string ( ) ,
174
+ ) ) ;
175
+
176
+ match res {
177
+ Ok ( _) => { } ,
178
+ Err ( e) => {
179
+ log:: error!( "dynamic import failed: {}" , e) ;
180
+ } ,
181
+ }
182
+
183
+ }
101
184
}
0 commit comments