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

Timers and modules #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

davidgraeff
Copy link

This PR adds module support (not binary modules, just other files imported via the ES6 module syntax) and the js timer API (setTimeout, clearTimeout).

I wasn't really happy with bindings.rs being that one big huge file and refactored. This led to a big PR diff, but I'm willing to split this into smaller change-sets if the overall structure is something that this project want to go forward with.

The changes in detail. First commit:

  • Patch makefile in build.rs to allow for cross compiling.
  • Add quickjs-libc.h to wrapper.h and regenerate bindings.rs for module support.
  • just: Let bindgen generate to src/bindings.rs
  • Use src/bindings.rs directly in lib.rs instead of including a file from OUT (faster compile time!)

Second commit:

  1. The bindings.rs file grew to one huge file with many different aspects covered. It is now split into value/marshal for ser/deserializing of JsValues and owned_value_ref for the OwnedValueRef type and utils for general utility methods.

  2. DroppableValue was used in two ways: Either as a OwnedValueRef with a context pointer (instead of a ContextWrapper like OwnedValueRef uses it) or for the Properties enumeration. DroppableValue has been completely removed instead and OwnedValueRef takes a context pointer now. For properties enumeration a new type has been introduced (see value/marshal.rs).

  3. impl ContextWrapper has been split into multiple impl ContextWrapper's, so that the callback API, timer API, module support each stay in their own rust-modules. bindings.rs is now purely for state keeping and the eval related API with about 500 LOC instead of 2k LOC.

  4. Compiling to bytecode and running from bytecode has been added.

  5. The timer API setTimeout/clearTimeout has been added. resolve_value required a few changes to "wait" for all timers to expire before returning.

  6. Allow callback function arguments for add_callback and create_callback APIs. A new OpaqueJsFunction type (a simple-type wrapper around OwnedValueRef) is introduced for this. Usage example:

fn register_state_listener(id: String, callback_function: OpaqueJsFunction) -> i32 {
    let _ = callback_function.invoke(vec![id]);
    0

}
context.add_callback("notifyOnStateChange", register_state_listener)?;
  1. Allow for a "generic" function argument for add_callback and create_callback APIs. This is useful if a js-api consumer is allowed to hand in a number, a string, a boolean. Usage example:
fn set_named_output(var_name: String, var_value: JsSimpleArgumentValue) -> JsValue {
    println!("setNamedOutputValue {} {:?}", var_name, var_value);
    JsValue::Null
}

David Gräff added 2 commits December 4, 2019 18:45
* Patch makefile in build.rs to allow for cross compiling.
* Add quickjs-libc.h to wrapper.h and regenerate bindings.rs for module support.
* just: Let bindgen generate to src/bindings.rs
* Use src/bindings.rs directly in lib.rs instead of including a file from OUT (faster compile time!)

Signed-off-by: David Gräff <[email protected]>
…earTimeout). Add module support. Add compile/run_bytecode methods.

1. The bindings.rs file grew to one huge file with many different aspects covered. It is now split into value/marshal for ser/deserializing of JsValues and owned_value_ref for the OwnedValueRef type and utils for general utility methods.

2. DroppableValue was used in two ways: Either as a OwnedValueRef with a context pointer (instead of a ContextWrapper like OwnedValueRef uses it) or for the Properties enumeration. DroppableValue has been completely removed instead and OwnedValueRef takes a context pointer now. For properties enumeration a new type has been introduced (see value/marshal.rs).

3. impl ContextWrapper has been split into multiple impl ContextWrapper's, so that the callback API, timer API, module support each stay in their own rust-modules. bindings.rs is now purely for state keeping and the eval related API with about 500 LOC instead of 2k LOC.

4. Compiling to bytecode and running from bytecode has been added.

5. The timer API setTimeout/clearTimeout has been added. resolve_value required a few changes to "wait" for all timers to expire before returning.

6. Allow callback function arguments for `add_callback` and `create_callback` APIs. A new OpaqueJsFunction type (a simple-type wrapper around OwnedValueRef) is introduced for this. Usage example:

fn register_state_listener(id: String, callback_function: OpaqueJsFunction) -> i32 {
    let _ = callback_function.invoke(vec![id]);
    0

}
context.add_callback("notifyOnStateChange", register_state_listener)?;

7. Allow for a "generic" function argument for `add_callback` and `create_callback` APIs. This is useful if a js-api consumer is allowed to hand in a number, a string, a boolean. Usage example:

fn set_named_output(var_name: String, var_value: JsSimpleArgumentValue) -> JsValue {
    println!("setNamedOutputValue {} {:?}", var_name, var_value);
    JsValue::Null
}

Signed-off-by: David Gräff <[email protected]>
@theduke
Copy link
Owner

theduke commented Dec 13, 2019

Hey sorry for the delay, I'll get to it this weekend.

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.

2 participants