Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto run executePendingJobs #34

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Holly-at-evg
Copy link

@Holly-at-evg Holly-at-evg commented May 13, 2024

there no need to manually execute executePendingJobs method, in https://github.com/justjake/quickjs-emscripten/?tab=readme-ov-file#promises, the author provide a way that can trigger on the promise is resolved using "promise.settled.then(vm.runtime.executePendingJobs)"

const vm = QuickJS.newContext()
const fakeFileSystem = new Map([["example.txt", "Example file content"]])

// Function that simulates reading data asynchronously
const readFileHandle = vm.newFunction("readFile", (pathHandle) => {
  const path = vm.getString(pathHandle)
  const promise = vm.newPromise()
  setTimeout(() => {
    const content = fakeFileSystem.get(path)
    promise.resolve(vm.newString(content || ""))
  }, 100)
  // IMPORTANT: Once you resolve an async action inside QuickJS,
  // call runtime.executePendingJobs() to run any code that was
  // waiting on the promise or callback.
  promise.settled.then(vm.runtime.executePendingJobs)
  return promise.handle
})
readFileHandle.consume((handle) => vm.setProp(vm.global, "readFile", handle))

// Evaluate code that uses `readFile`, which returns a promise
const result = vm.evalCode(`(async () => {
  const content = await readFile('example.txt')
  return content.toUpperCase()
})()`)
const promiseHandle = vm.unwrapResult(result)

// Convert the promise handle into a native promise and await it.
// If code like this deadlocks, make sure you are calling
// runtime.executePendingJobs appropriately.
const resolvedResult = await vm.resolvePromise(promiseHandle)
promiseHandle.dispose()
const resolvedHandle = vm.unwrapResult(resolvedResult)
console.log("Result:", vm.getString(resolvedHandle))
resolvedHandle.dispose()

@Holly-at-evg Holly-at-evg requested a review from rot1024 as a code owner May 13, 2024 14:29
@Pkcarreno
Copy link

Pkcarreno commented Oct 13, 2024

I think this could help to solve #32

Edit:
I did a quick review and did not contemplate certain things:

  • AsyncArena, BigInt and ArrayBuffer additions are cool, but I think they are outside the scope of the initial PR. So it should be a new PR
  • In the changes, there are important modifications to note, such as changing the package name (which shouldn't go) as well as changing the version number. And some other comments that should not be there to avoid future misunderstandings
  • The addition of AsyncArena is the one that should solve Doesn't work with asyncify #32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants