@@ -7,13 +7,11 @@ import assert from "node:assert"
77import { getQuickJS } from "./index.js"
88import type { JSModuleLoader , JSModuleNormalizer , QuickJSHandle , QuickJSContext } from "./index.js"
99
10- const DEBUG = false
10+ const DEBUG = true
1111
1212const ttyLog = ( ...args : unknown [ ] ) => {
1313 if ( DEBUG ) {
14- const fd = fs . openSync ( "/dev/tty" , "w" )
15- fs . writeSync ( fd , util . format ( ...args ) + "\n" )
16- fs . closeSync ( fd )
14+ console . error ( "[DEBUG]" , util . format ( ...args ) )
1715 }
1816}
1917
@@ -120,7 +118,7 @@ class QuickJSNodeModuleLoader {
120118 }
121119}
122120
123- test ( "quickjs-for-quickjs" , ( ) => {
121+ test ( "quickjs-for-quickjs" , async ( ) => {
124122 const moduleLoader = new QuickJSNodeModuleLoader ( )
125123 moduleLoader . mountImport ( {
126124 hostImport : "quickjs-emscripten-core" ,
@@ -143,20 +141,124 @@ test("quickjs-for-quickjs", () => {
143141 const logs : any [ ] = [ ]
144142 addConsoleGlobal ( context , logs )
145143
146- ttyLog ( "hi " )
144+ ttyLog ( "=== Starting quickjs-for-quickjs test === " )
147145
146+ // First, let's test if simple async functions work
147+ ttyLog ( "--- Test 1: Simple async function ---" )
148+ const simpleAsyncResult = context . evalCode (
149+ `
150+ globalThis.simpleTest = (async () => {
151+ console.log('async step 1')
152+ await Promise.resolve()
153+ console.log('async step 2')
154+ return 42
155+ })()
156+ ` ,
157+ "/simple-async.mjs" ,
158+ )
159+ context . unwrapResult ( simpleAsyncResult ) . dispose ( )
160+
161+ // Execute pending jobs and see how many were executed
162+ let jobResult = runtime . executePendingJobs ( )
163+ ttyLog ( "After simple async, executePendingJobs result:" , jobResult )
164+ ttyLog ( "Logs so far:" , logs )
165+
166+ // Check the Promise state
167+ const simpleTestHandle = context . getProp ( context . global , "simpleTest" )
168+ const simpleTestState = context . getPromiseState ( simpleTestHandle )
169+ ttyLog ( "simpleTest Promise state:" , simpleTestState )
170+ simpleTestHandle . dispose ( )
171+
172+ // Now test the full scenario with more logging
173+ ttyLog ( "--- Test 2: Full quickjs-for-quickjs ---" )
148174 const result = context . evalCode (
149175 `
150176import { newQuickJSWASMModuleFromVariant } from 'quickjs-emscripten-core/index.mjs'
151177import variant from '@jitl/quickjs-asmjs-mjs-release-sync/index.mjs'
152- globalThis.done = newQuickJSWASMModuleFromVariant(variant).then(QuickJS => {
153- const result = QuickJS.evalCode('1+2')
154- console.log('inner result', result)
155- })
178+
179+ console.log('starting main code')
180+ console.log('variant type:', typeof variant)
181+ console.log('variant keys:', Object.keys(variant))
182+
183+ globalThis.done = (async () => {
184+ try {
185+ console.log('async function started')
186+
187+ // Test what the variant loader returns
188+ console.log('calling variant.importModuleLoader...')
189+ const loaderPromise = variant.importModuleLoader()
190+ console.log('importModuleLoader returned:', typeof loaderPromise)
191+
192+ const loader = await loaderPromise
193+ console.log('loader awaited, type:', typeof loader)
194+
195+ console.log('calling loader()...')
196+ const modulePromise = loader()
197+ console.log('loader() returned:', typeof modulePromise)
198+
199+ const wasmModule = await modulePromise
200+ console.log('wasmModule awaited, type:', typeof wasmModule)
201+ console.log('wasmModule.type:', wasmModule?.type)
202+
203+ // Now create the full QuickJS instance
204+ console.log('calling newQuickJSWASMModuleFromVariant...')
205+ const QuickJS = await newQuickJSWASMModuleFromVariant(variant)
206+ console.log('QuickJS created:', typeof QuickJS)
207+
208+ const evalResult = QuickJS.evalCode('1+2')
209+ console.log('inner result', evalResult)
210+
211+ return evalResult
212+ } catch (error) {
213+ console.log('ERROR in async function:', String(error))
214+ console.log('ERROR stack:', error?.stack || 'no stack')
215+ throw error
216+ }
217+ })()
218+
219+ console.log('done promise created:', typeof globalThis.done)
156220` ,
157221 "/script.mjs" ,
158222 )
159223 context . unwrapResult ( result ) . dispose ( )
160- runtime . executePendingJobs ( )
161- assert . deepEqual ( logs , [ [ "inner result" , 3 ] ] )
224+
225+ // Execute pending jobs multiple times with logging
226+ ttyLog ( "After evalCode, executing pending jobs..." )
227+ for ( let i = 0 ; i < 10 ; i ++ ) {
228+ jobResult = runtime . executePendingJobs ( )
229+ ttyLog ( `executePendingJobs iteration ${ i } :` , jobResult )
230+
231+ // Check done Promise state
232+ const doneHandle = context . getProp ( context . global , "done" )
233+ const doneState = context . getPromiseState ( doneHandle )
234+ ttyLog ( `done Promise state at iteration ${ i } :` , doneState )
235+
236+ if ( doneState . type !== "pending" ) {
237+ ttyLog ( "Promise resolved/rejected, stopping loop" )
238+ if ( doneState . type === "fulfilled" ) {
239+ const value = context . dump ( doneState . value )
240+ ttyLog ( "Fulfilled with:" , value )
241+ doneState . value . dispose ( )
242+ } else if ( doneState . type === "rejected" ) {
243+ const error = context . dump ( doneState . error )
244+ ttyLog ( "Rejected with:" , error )
245+ doneState . error . dispose ( )
246+ }
247+ doneHandle . dispose ( )
248+ break
249+ }
250+ doneHandle . dispose ( )
251+
252+ // Small delay to let any host promises settle
253+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
254+ }
255+
256+ ttyLog ( "=== Final logs ===" )
257+ ttyLog ( "Logs array:" , logs )
258+
259+ // For now, just check that we eventually see "inner result"
260+ const innerResultLog = logs . find ( ( log : any [ ] ) => log [ 0 ] === "inner result" )
261+ ttyLog ( "Looking for 'inner result' log:" , innerResultLog )
262+ assert . ok ( innerResultLog , "Should have logged 'inner result'" )
263+ assert . strictEqual ( innerResultLog [ 1 ] , 3 , "Inner result should be 3" )
162264} )
0 commit comments