sciter-sdk / rust-sciter

Rust bindings for Sciter
https://sciter.com
MIT License
806 stars 76 forks source link

How to use `set_variable`? #133

Open thecodrr opened 2 years ago

thecodrr commented 2 years ago

I tried doing this:

    window
        .set_variable("hello", Value::from("world"))
        .expect("Could not set variable.");

But I don't know how to retrieve the set variable. Window.this.hello and globalThis.hello both return undefined. Even get_variable returns undefined.

pravic commented 2 years ago
    // set the global to all windows
    if let Err(e) = sciter::set_variable("svglobal", Value::from("hello, globals")) {
        // ignore this
        eprintln!("sciter.set_variable: {:?}", e);
    }

    frame.load_html(html, Some("example://minimal.htm"));

    // set the global only for this window
    if let Err(e) = frame.set_variable("svdocument", Value::from("hello, document")) {
        eprintln!("frame.set_variable: {:?}", e);
    }
globalThis.svdocument;
globalThis.svglobal;

Also, ignore the first error: https://sciter.com/forums/topic/hruntimeset_global_variable-always-returns-false/

pravic commented 2 years ago

And get:

    println!("document: {:?}", frame.get_variable("svglobal"));
    println!("document: {:?}", frame.get_variable("svdocument"));
    println!("location: {:?}", frame.get_variable("location"));

    // doesn't work:
    println!("Window.this: {:?}", frame.get_variable("Window.this"));
pravic commented 2 years ago

Keep in mind that this API was introduced (yet disabled) in Sciter 4.4.4.6. but properly enabled only in 4.4.8.26. Available in rust-sciter since 0.5.58, see 8507e9181b.

thecodrr commented 2 years ago

Tried doing as you suggested. No luck.

    sciter::set_library("./lib/libsciter-gtk.so").expect("Invalid library path.");

    println!("Version: {:?}", sciter::version());

    let mut frame = sciter::Window::new();
    // let mut root = frame.get_host().get_root().expect("hello");

    // set the global to all windows
    if let Err(e) = sciter::set_variable("svglobal", Value::from("hello, globals")) {
        // ignore this
        eprintln!("sciter.set_variable: {:?}", e);
    }

    frame.load_file("./host/main.htm");

    // set the global only for this window
    if let Err(e) = frame.set_variable("svdocument", Value::from("hello, document")) {
        eprintln!("frame.set_variable: {:?}", e);
    }

    println!("document: {:?}", frame.get_variable("svglobal"));
    println!("document: {:?}", frame.get_variable("svdocument"));
    println!("location: {:?}", frame.get_variable("location"));

    frame.run_app();

Result:

Version: "4.4.8.28"
INFO:TIS: HELLO!,undefined,undefined
frame.set_variable: ()
document: Err(())
document: Err(())
location: Err(())

The JS part:

console.log("HELLO!", globalThis.svdocument, globalThis.svglobal);

Edit: oh wait. From the output, i can see the variables are getting set after the initialization. Let me put this into an event handler.

pravic commented 2 years ago

From the output, i can see the variables are getting set after the initialization. Let me put this into an event handler

Well, on Windows load_file is synchronous, but it may differ on Linux. Any luck?

thecodrr commented 2 years ago

No, it's sync everywhere. The load_file function loads everything and since I am doing the console.log in a different script file which is then loaded from the main.htm file, it executes before everything else.

This is a good use case for the document_complete event handler. Is there a way to pass the frame to the EventHandler?

thecodrr commented 2 years ago

Okay, didn't work.

No idea why this is resulting in an error.

 // set the global only for this window
    if let Err(e) = frame.set_variable("svdocument", Value::from("hello, document")) {
        eprintln!("frame.set_variable: {:?}", e);
    }

Any way to see the error message?

pravic commented 2 years ago

Any way to see the error message?

Well, it does print something like "frame.set_variable: INVALID_PARAMETER". Nothing more: https://github.com/c-smile/sciter-js-sdk/blob/main/include/sciter-x-dom.h#L59-L79