-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,75 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use crate::jsapi::JS::{ | ||
CompileGlobalScriptToStencil2, DestroyFrontendContext, FrontendContext as RawFrontendContext, | ||
NewFrontendContext, ReadOnlyCompileOptions, | ||
}; | ||
use crate::rust::{transform_str_to_source_text, OwningCompileOptionsWrapper, Stencil}; | ||
use std::ops::Deref; | ||
use std::sync::Arc; | ||
use std::thread::{self, JoinHandle}; | ||
|
||
pub struct FrontendContext(*mut RawFrontendContext); | ||
|
||
unsafe impl Send for FrontendContext {} | ||
|
||
impl FrontendContext { | ||
pub fn new() -> Self { | ||
Self(unsafe { NewFrontendContext() }) | ||
} | ||
} | ||
|
||
impl Deref for FrontendContext { | ||
type Target = *mut RawFrontendContext; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Drop for FrontendContext { | ||
fn drop(&mut self) { | ||
unsafe { DestroyFrontendContext(self.0) } | ||
} | ||
} | ||
|
||
pub struct OffThreadToken(JoinHandle<Option<Stencil>>); | ||
|
||
impl OffThreadToken { | ||
/// Obtains result | ||
/// | ||
/// Blocks until completion | ||
pub fn finish(self) -> Option<Stencil> { | ||
self.0.join().ok().flatten() | ||
} | ||
} | ||
|
||
/// Creates a new thread and starts compilation there | ||
/// | ||
/// Callback receives stencil that can either consumed or returned | ||
pub fn compile_to_stencil_offthread<F>( | ||
options: *const ReadOnlyCompileOptions, | ||
source: Arc<String>, | ||
callback: F, | ||
) -> OffThreadToken | ||
where | ||
F: FnOnce(Stencil) -> Option<Stencil> + Send + 'static, | ||
{ | ||
let fc = FrontendContext::new(); | ||
let options = OwningCompileOptionsWrapper::new_for_fc(&fc, options); | ||
OffThreadToken( | ||
thread::Builder::new() | ||
.name("OffThread Compile".to_string()) | ||
.spawn(move || { | ||
callback(unsafe { | ||
Stencil::from_raw(CompileGlobalScriptToStencil2( | ||
*fc, | ||
options.read_only(), | ||
&mut transform_str_to_source_text(&source) as *mut _, | ||
)) | ||
}) | ||
}) | ||
.unwrap(), | ||
) | ||
} |
This file contains 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 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,70 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use std::ptr; | ||
use std::sync::mpsc::channel; | ||
use std::sync::Arc; | ||
|
||
use mozjs::jsapi::{ | ||
InstantiateGlobalStencil, InstantiateOptions, JSAutoRealm, JS_NewGlobalObject, | ||
OnNewGlobalHookOption, | ||
}; | ||
use mozjs::jsval::UndefinedValue; | ||
use mozjs::offthread::compile_to_stencil_offthread; | ||
use mozjs::rooted; | ||
use mozjs::rust::{ | ||
wrappers::JS_ExecuteScript, CompileOptionsWrapper, JSEngine, RealmOptions, Runtime, | ||
SIMPLE_GLOBAL_CLASS, | ||
}; | ||
|
||
#[test] | ||
fn offthread() { | ||
let engine = JSEngine::init().unwrap(); | ||
let runtime = Runtime::new(engine.handle()); | ||
let context = runtime.cx(); | ||
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook; | ||
let c_option = RealmOptions::default(); | ||
|
||
unsafe { | ||
rooted!(in(context) let global = JS_NewGlobalObject( | ||
context, | ||
&SIMPLE_GLOBAL_CLASS, | ||
ptr::null_mut(), | ||
h_option, | ||
&*c_option, | ||
)); | ||
|
||
let _ac = JSAutoRealm::new(context, global.get()); | ||
|
||
let src = Arc::new("1 + 1".to_string()); | ||
let options = CompileOptionsWrapper::new(context, "", 1); | ||
let options_ptr = options.ptr as *const _; | ||
let (sender, receiver) = channel(); | ||
let offthread_token = compile_to_stencil_offthread(options_ptr, src, move |stencil| { | ||
sender.send(stencil).unwrap(); | ||
None | ||
}); | ||
|
||
let stencil = receiver.recv().unwrap(); | ||
|
||
assert!(offthread_token.finish().is_none()); | ||
|
||
let options = InstantiateOptions { | ||
skipFilenameValidation: false, | ||
hideScriptFromDebugger: false, | ||
deferDebugMetadata: false, | ||
}; | ||
rooted!(in(context) let script = InstantiateGlobalStencil( | ||
context, | ||
&options, | ||
*stencil, | ||
ptr::null_mut(), | ||
)); | ||
|
||
rooted!(in(context) let mut rval = UndefinedValue()); | ||
let result = JS_ExecuteScript(context, script.handle(), rval.handle_mut()); | ||
assert!(result); | ||
assert_eq!(rval.get().to_int32(), 2); | ||
} | ||
} |