Giter Site home page Giter Site logo

Array support in scope about rhai HOT 9 CLOSED

rikvdkleij avatar rikvdkleij commented on June 10, 2024
Array support in scope

from rhai.

Comments (9)

rikvdkleij avatar rikvdkleij commented on June 10, 2024 1

You are not using new_raw by any chance?

Yes, an engine is created with Engine::new_raw() because of safety reasons and side-effects as printing should not be possible.

So it looks like that to solve this the BasicArrayPackage package has to be registered in the engine.

That's confusing because by using the raw engine (and so only the core package) arrays are supported but their functions not....

But I'm glad it's now clarified. Thanks for your help!

from rhai.

rikvdkleij avatar rikvdkleij commented on June 10, 2024 1

Actually, raw engines do not have anything to do with security and safety. You can always disable printing etc. by calling on_print and on_debug and put in stubs. You can also disable module resolution by setting it to an empty resolver. All these you can do on a standard Engine.

Okay, but safety starts with disabling everything and only enable what you need 😄

from rhai.

schungx avatar schungx commented on June 10, 2024

You can convert it first into an Array by making it a Dynamic.

let v : Dynamic = value.into();

from rhai.

rikvdkleij avatar rikvdkleij commented on June 10, 2024

Thanks for your quick reply.

Ok, that was not clear from the documentation:
https://rhai.rs/book/language/arrays.html#arrays

The Rust type of a Rhai array is rhai::Array which is an alias to Vec<Dynamic>.

So the documentation states the elements have to be Dynamic value.

Unfortunately, it still not works:

                let codes: Vec<String> = ....;
                let foo: Dynamic = codes.into();
                scope.push_constant("bar", foo);

Script contains:

bar.contains("x")

Error:

ErrorFunctionNotFound", error_message: "Function not found: contains (array, &str | ImmutableString | String)

from rhai.

schungx avatar schungx commented on June 10, 2024

Hhhmmm... It properly recognizes the type as array. So the data is correct.

Just the function is not found, which is strange.

Can you try: "x" in bar

from rhai.

rikvdkleij avatar rikvdkleij commented on June 10, 2024

Can you try: "x" in bar

Gives the same error.

 "Function not found: contains (array, &str | ImmutableString | String) 

AFAICS the Rhai runtime still "interpretes" bar as a Rust type.

from rhai.

rikvdkleij avatar rikvdkleij commented on June 10, 2024

O wait, type_of() now gives array for bar.

That differs with my previous attempts.

from rhai.

schungx avatar schungx commented on June 10, 2024

O wait, type_of() now gives array for bar.

That differs with my previous attempts.

Yes, that's the point. The data type is correct.

Just the function is not found.

You are not using new_raw by any chance?

Or can you share a reproducible snippet?

from rhai.

schungx avatar schungx commented on June 10, 2024

Yes, an engine is created with Engine::new_raw() because of safety reasons and side-effects as printing should not be possible.

Actually, raw engines do not have anything to do with security and safety. You can always disable printing etc. by calling on_print and on_debug and put in stubs. You can also disable module resolution by setting it to an empty resolver. All these you can do on a standard Engine.

Raw engines are for environments with limited memory, so they don't load the entire standard library, and you need to pick-and-choose the libraries you need.

For your concerns you may consider:

// Create the new scripting Engine
let mut engine = Self::new();
// Disable stuff with side effects
engine
    .set_module_resolver(rhai::module::resolvers::DummyModuleResolver::new())
    .on_print(|_| {})
    .on_debug(|_, _, _| {});
// Make engine immutable
let engine = engine;

from rhai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.