Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andrieshiemstra committed May 8, 2023
1 parent 4092e14 commit dbd9afe
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 106 deletions.
6 changes: 3 additions & 3 deletions src/facades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl QuickjsRuntimeFacadeInner {
/// use quickjs_runtime::quickjs_utils::primitives;
/// let rt = QuickJsRuntimeBuilder::new().build();
/// let res = rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// // here you are in the worker thread and you can use the quickjs_utils
/// let val_ref = q_ctx.eval(Script::new("test.es", "(11 * 6);")).ok().expect("script failed");
/// primitives::to_i32(&val_ref).ok().expect("could not get i32")
Expand Down Expand Up @@ -379,7 +379,7 @@ impl QuickJsRuntimeFacade {
/// use quickjs_runtime::quickjs_utils::primitives;
/// let rt = QuickJsRuntimeBuilder::new().build();
/// let res = rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// // here you are in the worker thread and you can use the quickjs_utils
/// let val_ref = q_ctx.eval(Script::new("test.es", "(11 * 6);")).ok().expect("script failed");
/// primitives::to_i32(&val_ref).ok().expect("could not get i32")
Expand Down Expand Up @@ -1142,7 +1142,7 @@ pub mod tests {

rt.exe_rt_task_in_event_loop(|q_js_rt| {
//q_js_rt.run_pending_jobs_if_any();
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let r = q_ctx.eval(Script::new(
"test_async.es",
"let f = async function(){let p = new Promise((resolve, reject) => {resolve(12345);}); const p2 = await p; return p2}; f();",
Expand Down
2 changes: 1 addition & 1 deletion src/features/set_timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub mod tests {
std::thread::sleep(Duration::from_secs(6));

let i = rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let global = get_global_q(q_ctx);
let ti_num = get_property_q(q_ctx, &global, "__ti_num__")
.ok()
Expand Down
12 changes: 6 additions & 6 deletions src/quickjs_utils/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use libquickjs_sys as q;
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let obj_ref = q_ctx.eval(Script::new("is_array_test.es", "([1, 2, 3]);")).ok().expect("script failed");
/// let is_array = arrays::is_array_q(q_ctx, &obj_ref);
/// assert!(is_array);
Expand All @@ -39,7 +39,7 @@ pub unsafe fn is_array(context: *mut q::JSContext, obj_ref: &QuickJsValueAdapter
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let obj_ref = q_ctx.eval(Script::new("get_length_test.es", "([1, 2, 3]);")).ok().expect("script failed");
/// let len = arrays::get_length_q(q_ctx, &obj_ref).ok().expect("could not get length");
/// assert_eq!(len, 3);
Expand Down Expand Up @@ -76,7 +76,7 @@ pub unsafe fn get_length(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// // create a method to pass our new array to
/// q_ctx.eval(Script::new("create_array_test.es", "this.create_array_func = function(arr){return arr.length;};")).ok().expect("script failed");
/// // create a new array
Expand Down Expand Up @@ -118,7 +118,7 @@ pub unsafe fn create_array(context: *mut q::JSContext) -> Result<QuickJsValueAda
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// // get an Array from script
/// let arr_ref = q_ctx.eval(Script::new("set_element_test.es", "([1, 2, 3]);")).ok().expect("script failed");
/// // add some values
Expand Down Expand Up @@ -171,7 +171,7 @@ pub unsafe fn set_element(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// // get an Array from script
/// let arr_ref = q_ctx.eval(Script::new("get_element_test.es", "([1, 2, 3]);")).ok().expect("script failed");
/// // get a value, the 3 in this case
Expand Down Expand Up @@ -220,7 +220,7 @@ pub mod tests {
fn test_array() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let arr = create_array_q(q_ctx).ok().unwrap();
assert_eq!(arr.get_ref_count(), 1);

Expand Down
2 changes: 1 addition & 1 deletion src/quickjs_utils/bigints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub mod tests {
fn test_bigint() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let bi_ref =
new_bigint_str_q(q_ctx, "345346345645234564536345345345345456534783448567")
.expect("could not create bigint from str");
Expand Down
12 changes: 6 additions & 6 deletions src/quickjs_utils/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::os::raw::c_void;
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// unsafe {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let func_res = compile(q_ctx.context, Script::new("test_func.es", "let a = 7; let b = 5; a * b;"));
/// let func = func_res.ok().expect("func compile failed");
/// let run_res = run_compiled_function(q_ctx.context, &func);
Expand Down Expand Up @@ -108,7 +108,7 @@ pub unsafe fn run_compiled_function(
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// unsafe {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let func_res = compile(q_ctx.context, Script::new("test_func.es", "let a = 7; let b = 5; a * b;"));
/// let func = func_res.ok().expect("func compile failed");
/// let bytecode: Vec<u8> = to_bytecode(q_ctx.context, &func);
Expand Down Expand Up @@ -202,7 +202,7 @@ pub mod tests {
let rt = init_test_rt();

rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let func_res = unsafe {
compile(
q_ctx.context,
Expand Down Expand Up @@ -236,7 +236,7 @@ pub mod tests {
fn test_bytecode() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| unsafe {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let func_res = compile(
q_ctx.context,
Script::new(
Expand Down Expand Up @@ -269,7 +269,7 @@ pub mod tests {
fn test_bytecode_bad_compile() {
let rt = QuickJsRuntimeBuilder::new().build();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

let func_res = unsafe {
compile(
Expand All @@ -288,7 +288,7 @@ pub mod tests {
fn test_bytecode_bad_run() {
let rt = QuickJsRuntimeBuilder::new().build();
rt.exe_rt_task_in_event_loop(|q_js_rt| unsafe {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

let func_res = compile(
q_ctx.context,
Expand Down
2 changes: 1 addition & 1 deletion src/quickjs_utils/dates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub mod tests {
fn test_date() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let date_ref = dates::new_date_q(q_ctx).expect("new_date failed");
assert!(is_date_q(q_ctx, &date_ref));

Expand Down
2 changes: 1 addition & 1 deletion src/quickjs_utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub mod tests {

let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

q_ctx
.eval(Script::new(
Expand Down
18 changes: 9 additions & 9 deletions src/quickjs_utils/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ pub(crate) fn init_statics() {
/// use quickjs_runtime::quickjs_utils::objects::set_property_q;
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// // create a function which always returns 1253
/// let func_obj = new_function_q(q_ctx, "myFunc7654", |_q_ctx, _this, _args|{Ok(from_i32(1253))}, 0).ok().unwrap();
/// // store as a global member so script can call it
Expand Down Expand Up @@ -680,7 +680,7 @@ pub mod tests {
pub fn test_invoke() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let obj_ref = q_ctx
.eval(Script::new(
"test_to_invoke.es",
Expand Down Expand Up @@ -709,7 +709,7 @@ pub mod tests {
pub fn test_ret_refcount() {
let rt = init_test_rt();
let io = rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let func_ref = q_ctx
.eval(Script::new(
"test_ret_refcount.es",
Expand Down Expand Up @@ -756,7 +756,7 @@ pub mod tests {
pub fn test_to_string() {
let rt = init_test_rt();
let io = rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let i = primitives::from_i32(480);
let i_s = call_to_string_q(q_ctx, &i)
.ok()
Expand All @@ -779,7 +779,7 @@ pub mod tests {
pub fn test_call() {
let rt = init_test_rt();
let io = rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let func_ref = q_ctx
.eval(Script::new(
"test_call.es",
Expand Down Expand Up @@ -817,7 +817,7 @@ pub mod tests {
rt.eval_sync(None, Script::new("test_callback1.es", "let test_callback_563 = function(cb){console.log('before invoke cb');let result = cb(1, true, 'foobar');console.log('after invoke cb. got:' + result);};")).ok().expect("script failed");

rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let mut cb_ref = new_function_q(
q_ctx,
"cb",
Expand Down Expand Up @@ -860,7 +860,7 @@ pub mod tests {

rt.exe_rt_task_in_event_loop(|q_js_rt| {

let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

let func_ref = q_ctx.eval(Script::new(
"test_callback845.es",
Expand Down Expand Up @@ -901,7 +901,7 @@ pub mod tests {
let rt = init_test_rt();

let err = rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

q_ctx
.install_function(
Expand Down Expand Up @@ -1008,7 +1008,7 @@ pub mod tests2 {
fn test_function() {
let rt = QuickJsRuntimeBuilder::new().build();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();
let func = new_function_q(
q_ctx,
"test_func",
Expand Down
2 changes: 1 addition & 1 deletion src/quickjs_utils/interrupthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub mod tests {
let rt = QuickJsRuntimeBuilder::new()
.set_interrupt_handler(move |qjs_rt| {
log::debug!("interrupt_handler called / 1");
let script_name = get_script_or_module_name_q(qjs_rt.get_main_context());
let script_name = get_script_or_module_name_q(qjs_rt.get_main_realm());
match script_name {
Ok(script_name) => {
log::debug!("interrupt_handler called: {}", script_name);
Expand Down
4 changes: 2 additions & 2 deletions src/quickjs_utils/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub unsafe fn parse(
/// use quickjs_runtime::quickjs_utils::{json, objects, primitives};
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let obj_ref = objects::create_object_q(q_ctx).ok().unwrap();
/// objects::set_property_q(q_ctx, &obj_ref, "a", &primitives::from_i32(741)).ok().unwrap();
/// let str_ref = json::stringify_q(q_ctx, &obj_ref, None).ok().unwrap();
Expand Down Expand Up @@ -135,7 +135,7 @@ pub mod tests {
fn test_json() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

let obj = objects::create_object_q(q_ctx).ok().unwrap();
objects::set_property_q(q_ctx, &obj, "a", &primitives::from_i32(532))
Expand Down
20 changes: 10 additions & 10 deletions src/quickjs_utils/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use libquickjs_sys as q;
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// });
/// ```
Expand Down Expand Up @@ -55,7 +55,7 @@ pub unsafe fn is_map(ctx: *mut q::JSContext, obj: &QuickJsValueAdapter) -> Resul
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -93,7 +93,7 @@ pub unsafe fn set(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -131,7 +131,7 @@ pub unsafe fn get(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -169,7 +169,7 @@ pub unsafe fn delete(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -208,7 +208,7 @@ pub unsafe fn has(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -239,7 +239,7 @@ pub unsafe fn size(ctx: *mut q::JSContext, map: &QuickJsValueAdapter) -> Result<
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -271,7 +271,7 @@ pub unsafe fn clear(ctx: *mut q::JSContext, map: &QuickJsValueAdapter) -> Result
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -311,7 +311,7 @@ pub unsafe fn keys<C: Fn(QuickJsValueAdapter) -> Result<R, JsError>, R>(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down Expand Up @@ -351,7 +351,7 @@ pub unsafe fn values<C: Fn(QuickJsValueAdapter) -> Result<R, JsError>, R>(
///
/// let rt = QuickJsRuntimeBuilder::new().build();
/// rt.exe_rt_task_in_event_loop(|q_js_rt| {
/// let q_ctx = q_js_rt.get_main_context();
/// let q_ctx = q_js_rt.get_main_realm();
/// let my_map: QuickJsValueAdapter = new_map_q(q_ctx).ok().unwrap();
/// let key = primitives::from_i32(12);
/// let value = primitives::from_i32(23);
Expand Down
2 changes: 1 addition & 1 deletion src/quickjs_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub mod tests {
fn test_global() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let q_ctx = q_js_rt.get_main_realm();

let ct = get_global_q(q_ctx).get_ref_count();
for _ in 0..5 {
Expand Down
Loading

0 comments on commit dbd9afe

Please sign in to comment.