-
Notifications
You must be signed in to change notification settings - Fork 23
Quickjs #132
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
Draft
CedricGuillemet
wants to merge
11
commits into
BabylonJS:main
Choose a base branch
from
CedricGuillemet:quickjs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Quickjs #132
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
76ce806
QuickJS experiment
CedricGuillemet c7dfc92
CI pipelines
CedricGuillemet a3facf0
removed js
CedricGuillemet a3942fd
job name
CedricGuillemet 6b694d0
cleanup cmake scripts
CedricGuillemet 08100a2
warnings
CedricGuillemet a9f3e39
fix prototype
CedricGuillemet 4c9b6d8
forced quickjs promise continuation
CedricGuillemet 293b97c
fix performance
CedricGuillemet 705d0e3
fix some leaks
CedricGuillemet a66991c
release build to avoid asserts
CedricGuillemet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include "AppRuntime.h" | ||
| #include <napi/env.h> | ||
|
|
||
| #ifdef _WIN32 | ||
| #pragma warning(push) | ||
| // cast from int64 to int32 | ||
| #pragma warning(disable : 4244) | ||
| #endif | ||
| #include <quickjs.h> | ||
| #ifdef _WIN32 | ||
| #pragma warning(pop) | ||
| #endif | ||
|
|
||
| namespace Babylon | ||
| { | ||
| void AppRuntime::RunEnvironmentTier(const char* /*executablePath*/) | ||
| { | ||
| // Create the runtime. | ||
| JSRuntime* runtime = JS_NewRuntime(); | ||
| if (!runtime) | ||
| { | ||
| throw std::runtime_error{"Failed to create QuickJS runtime"}; | ||
| } | ||
|
|
||
| // Create the context. | ||
| JSContext* context = JS_NewContext(runtime); | ||
| if (!context) | ||
| { | ||
| JS_FreeRuntime(runtime); | ||
| throw std::runtime_error{"Failed to create QuickJS context"}; | ||
| } | ||
|
|
||
| // Use the context within a scope. | ||
| { | ||
| Napi::Env env = Napi::Attach(context); | ||
|
|
||
| Run(env); | ||
|
|
||
| Napi::Detach(env); | ||
| } | ||
|
|
||
| // Destroy the context and runtime. | ||
| JS_FreeContext(context); | ||
| JS_FreeRuntime(runtime); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| #include "WorkQueue.h" | ||
|
|
||
| #ifdef USE_QUICKJS | ||
| #ifdef _WIN32 | ||
| #pragma warning(push) | ||
| // cast from int64 to int32 | ||
| #pragma warning(disable : 4244) | ||
| #endif | ||
| #include <quickjs.h> | ||
| #ifdef _WIN32 | ||
| #pragma warning(pop) | ||
| #endif | ||
| #endif | ||
|
|
||
| namespace Babylon | ||
| { | ||
| WorkQueue::WorkQueue(std::function<void()> threadProcedure) | ||
|
|
@@ -42,6 +54,15 @@ namespace Babylon | |
| while (!m_cancelSource.cancelled()) | ||
| { | ||
| m_dispatcher.blocking_tick(m_cancelSource); | ||
| #ifdef USE_QUICKJS | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How to do it properly? |
||
| // Process QuickJS microtasks (promise callbacks, etc.) | ||
| JSContext* pending_ctx; | ||
| int result; | ||
| while ((result = JS_ExecutePendingJob(JS_GetRuntime(Napi::GetContext(env)), &pending_ctx)) > 0) | ||
| { | ||
| // Keep draining the microtask queue | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| m_dispatcher.clear(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| #include <napi/napi.h> | ||
| struct JSContext; | ||
|
|
||
| namespace Napi | ||
| { | ||
| Napi::Env Attach(JSContext* context); | ||
|
|
||
| void Detach(Napi::Env); | ||
|
|
||
| Napi::Value Eval(Napi::Env env, const char* source, const char* sourceUrl); | ||
|
|
||
| JSContext* GetContext(Napi::Env); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include <napi/env.h> | ||
| #include "js_native_api_quickjs.h" | ||
| #include <quickjs.h> | ||
|
|
||
| namespace Napi | ||
| { | ||
| Env Attach(JSContext* context) | ||
| { | ||
| napi_env env_ptr{new napi_env__}; | ||
| env_ptr->context = context; | ||
| env_ptr->current_context = env_ptr->context; | ||
|
|
||
| // Get Object.prototype.hasOwnProperty | ||
| JSValue global = JS_GetGlobalObject(context); | ||
| JSValue object = JS_GetPropertyStr(context, global, "Object"); | ||
| if (!JS_IsException(object) && JS_IsObject(object)) | ||
| { | ||
| JSValue prototype = JS_GetPrototype(context, object); | ||
| if (!JS_IsException(prototype) && JS_IsObject(prototype)) | ||
| { | ||
| JSValue hasOwnProperty = JS_GetPropertyStr(context, prototype, "hasOwnProperty"); | ||
| if (!JS_IsException(hasOwnProperty)) | ||
| { | ||
| env_ptr->has_own_property_function = hasOwnProperty; | ||
| } | ||
| else | ||
| { | ||
| JS_FreeValue(context, hasOwnProperty); | ||
| } | ||
| JS_FreeValue(context, prototype); | ||
| } | ||
| else if (JS_IsException(prototype)) | ||
| { | ||
| JS_FreeValue(context, prototype); | ||
| } | ||
| JS_FreeValue(context, object); | ||
| } | ||
| else if (JS_IsException(object)) | ||
| { | ||
| JS_FreeValue(context, object); | ||
| } | ||
| JS_FreeValue(context, global); | ||
|
|
||
| return {env_ptr}; | ||
| } | ||
|
|
||
| void Detach(Env env) | ||
| { | ||
| napi_env env_ptr{env}; | ||
| if (env_ptr) | ||
| { | ||
| if (!JS_IsUndefined(env_ptr->has_own_property_function)) | ||
| { | ||
| JS_FreeValue(env_ptr->context, env_ptr->has_own_property_function); | ||
| } | ||
| delete env_ptr; | ||
| } | ||
| } | ||
|
|
||
| JSContext* GetContext(Env env) | ||
| { | ||
| napi_env env_ptr{env}; | ||
| return env_ptr->context; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To remove