Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 19 additions & 32 deletions libfjs/src/api/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ impl JsEngine {
self.begin_init()?;

let bridge = Arc::new(bridge);
let attachment = self.context.global_attachment.clone();

let init_result = self
.context
.ctx
.async_with(async |ctx| {
if let Some(attachment) = &self.context.global_attachment
.with_js(async move |ctx| {
if let Some(attachment) = &attachment
&& let Err(e) = attachment.attach(&ctx)
{
return Err(anyhow!("Failed to attach global context: {}", e));
Expand Down Expand Up @@ -389,11 +389,11 @@ impl JsEngine {
pub async fn init_without_bridge(&self) -> anyhow::Result<()> {
self.begin_init()?;

let attachment = self.context.global_attachment.clone();
let init_result = self
.context
.ctx
.async_with(async |ctx| {
if let Some(attachment) = &self.context.global_attachment
.with_js(async move |ctx| {
if let Some(attachment) = &attachment
&& let Err(e) = attachment.attach(&ctx)
{
return Err(anyhow!("Failed to attach global context: {}", e));
Expand Down Expand Up @@ -439,8 +439,7 @@ impl JsEngine {
if previous_state == STATE_CREATED || previous_state == STATE_RUNNING {
let _ = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async |ctx| {
let globals = ctx.globals();
let _ = globals.remove("fjs");
Ok::<(), anyhow::Error>(())
Expand Down Expand Up @@ -500,8 +499,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
let res = ctx.eval_with_options(source_code, options.into());
result_from_promise(&ctx, res).await
})
Expand Down Expand Up @@ -539,8 +537,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if is_dynamic_module_loaded(&ctx, &module.name) {
return JsResult::Err(JsError::module(
Some(module.name),
Expand Down Expand Up @@ -591,8 +588,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
let conflicts: Vec<_> = modules
.iter()
.filter(|module| is_dynamic_module_loaded(&ctx, &module.name))
Expand Down Expand Up @@ -682,8 +678,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if is_dynamic_module_loaded(&ctx, &module_name) {
return JsResult::Err(JsError::module(
Some(module_name),
Expand Down Expand Up @@ -746,8 +741,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
let conflicts: Vec<_> = resolved_modules
.iter()
.filter(|(name, _)| is_dynamic_module_loaded(&ctx, name))
Expand Down Expand Up @@ -828,8 +822,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if is_dynamic_module_loaded(&ctx, &module_name) {
return JsResult::Err(JsError::module(
Some(module_name),
Expand Down Expand Up @@ -890,8 +883,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if is_dynamic_module_loaded(&ctx, &module_name) {
return JsResult::Err(JsError::module(
Some(module_name),
Expand Down Expand Up @@ -1015,8 +1007,7 @@ impl JsEngine {
let bytecode = script.bytes;
let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
let res = eval_script_bytecode(&ctx, &script_name, &bytecode);
result_from_maybe_promise(&ctx, res).await
})
Expand Down Expand Up @@ -1044,8 +1035,7 @@ impl JsEngine {

let result = self
.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if let Some(storage) = ctx.userdata::<DynamicModuleStorage>() {
let loaded: HashSet<_> =
get_loaded_dynamic_module_names(&ctx).into_iter().collect();
Expand Down Expand Up @@ -1083,8 +1073,7 @@ impl JsEngine {
self.ensure_running()?;

self.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if let Some(storage) = ctx.userdata::<DynamicModuleStorage>() {
let mut modules: Vec<_> = storage.read().unwrap().keys().cloned().collect();
modules.sort();
Expand Down Expand Up @@ -1140,8 +1129,7 @@ impl JsEngine {
self.ensure_running()?;

self.context
.ctx
.async_with(async |ctx| {
.with_js(async move |ctx| {
if let Some(storage) = ctx.userdata::<DynamicModuleStorage>() {
Ok(storage.read().unwrap().contains_key(&module_name))
} else {
Expand Down Expand Up @@ -1225,8 +1213,7 @@ impl JsEngine {
let params = params.unwrap_or_default();
let result = self
.context
.ctx
.async_with(async |ctx| call_module_method(&ctx, module, method, params).await)
.with_js(async move |ctx| call_module_method(&ctx, module, method, params).await)
.await;

result.into_result()
Expand Down
Loading