A safe JavaScript interop tool.
///|
typealias @jmop.(Object, Promise, Function)
///|
fn init {
@jmop.global_this.set(
"test",
Object::new({
"number1": 100,
"number2": 50,
"object": Object::new({
"add": Function::new(fn(x : Int, y) { x + y }),
"sub": Function::new(fn(x : Int, y) { x - y }),
}),
}),
) catch {
e => println(e)
}
}///|
test "object and function" {
let js_obj = @jmop.global_this.get_object("test")
let n1 = js_obj.get_number("number1")
let n2 = js_obj.get_number("number2")
inspect(js_obj.get_object("object").call("add", [n1, n2]), content="150")
inspect(js_obj.get_object("object").call("sub", [n1, n2]), content="50")
inspect(js_obj.get_object("object").call("add", ["123", 1]), content="1231")
}///|
test "promise" {
@jmop.try_launch(() => {
let js_promise = Promise::new(1)
let r = js_promise
.map(x => x + 1)
.bind(x => Promise::new(x.to_string()))
.wait()
println(r) // 2
})
}///|
test "fail-fast: get undefined field" {
global_this.get_object("test").get("undefined_obj") |> ignore
}
///|
test "fail-fast: create nested promise" {
@jmop.try_launch(() => Promise::new(Promise::new(1)).wait() |> ignore)
}impl IsStringOrSymbol for Stringimpl IsAny for AggregateErrorimpl IsError for AggregateErrorimpl IsObject for AggregateErrorimpl IsValue for AggregateErrorimpl Show for AggregateErrorimpl IsAny for InternalErrorimpl IsError for InternalErrorimpl IsObject for InternalErrorimpl IsValue for InternalErrorimpl Show for InternalErrorimpl Show for InvalidCastimpl Show for InvalidFieldimpl IsAny for RangeErrorimpl IsError for RangeErrorimpl IsObject for RangeErrorimpl IsValue for RangeErrorimpl Show for RangeErrorimpl IsAny for ReferenceErrorimpl IsError for ReferenceErrorimpl IsObject for ReferenceErrorimpl IsValue for ReferenceErrorimpl Show for ReferenceErrorimpl IsAny for SyntaxErrorimpl IsError for SyntaxErrorimpl IsObject for SyntaxErrorimpl IsValue for SyntaxErrorimpl Show for SyntaxErrortype Anytype Error_type Functionimpl IsStringOrFunctionOrObject for Functionpub type JsTypeErrortype Nullable[T]#callsite(autofill(loc))
fn[T] Nullable::unwrap(self : Nullable[T], loc~ : SourceLoc) -> T raise UnwrapNullpub type Objectimpl IsStringOrFunctionOrObject for Object#callsite(autofill(loc))
fn Object::get(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Value raise#callsite(autofill(loc))
fn Object::get_bigint(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Bigint raise#callsite(autofill(loc))
fn Object::get_boolean(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Bool raise#callsite(autofill(loc))
fn Object::get_function(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Function raise#callsite(autofill(loc))
fn Object::get_method(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Function raise#callsite(autofill(loc))
fn Object::get_number(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Double raise#callsite(autofill(loc))
fn Object::get_object(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Object raise#callsite(autofill(loc))
fn Object::get_string(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> String raise#callsite(autofill(loc))
fn Object::get_symbol(self : Object, prop : IsStringOrSymbol, loc~ : SourceLoc) -> Symbol raisetype Optional[T]#callsite(autofill(loc))
fn[T] Optional::unwrap(self : Optional[T], loc~ : SourceLoc) -> T raise UnwrapUndefinedtype Promise[T]pub type Value#callsite(autofill(loc))
fn Value::to_bigint(self : Value, loc~ : SourceLoc) -> Bigint raise InvalidCast#callsite(autofill(loc))
fn Value::to_boolean(self : Value, loc~ : SourceLoc) -> Bool raise InvalidCast#callsite(autofill(loc))
fn Value::to_error(self : Value, loc~ : SourceLoc) -> Error_ raise InvalidCast#callsite(autofill(loc))
fn Value::to_function(self : Value, loc~ : SourceLoc) -> Function raise InvalidCast#callsite(autofill(loc))
fn Value::to_number(self : Value, loc~ : SourceLoc) -> Double raise InvalidCast#callsite(autofill(loc))
fn Value::to_object(self : Value, loc~ : SourceLoc) -> Object raise InvalidCast#callsite(autofill(loc))
fn Value::to_string(self : Value, loc~ : SourceLoc) -> String raise InvalidCastA safe JavaScript interop tool.