sciter-sdk / rust-sciter

Rust bindings for Sciter

Home Page:https://sciter.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use `set_variable`?

thecodrr opened this issue · comments

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.

	// 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/

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"));

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 8507e91.

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.

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?

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?

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?

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