Coder Social home page Coder Social logo

Simplifying Function Binding about boa HOT 4 OPEN

ahaoboy avatar ahaoboy commented on May 1, 2024
Simplifying Function Binding

from boa.

Comments (4)

jedel1043 avatar jedel1043 commented on May 1, 2024

See #3770 for the main discussion, and @hansl is currently working on precisely this. (Which we are really grateful for!)

from boa.

ahaoboy avatar ahaoboy commented on May 1, 2024

There are some differences, I need to add the rust function to js in a way similar to rquickjs.

fn add(a: i32, b: i32) -> i32 {
    a + b
}

global
  .set(
      "add",
      Function::new(ctx.clone(), add )
          .unwrap()
          .with_name("add")
          .unwrap(),
  )

I wanted a way to add rust functions directly using register_global_property

fn add(a: i32, b: i32) -> i32 {
    a + b
}

 context
    .register_global_property(js_string!( "add" ),
    add,
    Attribute::all())
    .unwrap();

In mpv-boa, I've implemented a similar macros , but it's not perfect and still requires the use of macros to generate a boa-style function

from boa.

hansl avatar hansl commented on May 1, 2024

With my PR that example should work almost right out. I think the only thing might be missing a Into<JsValue> for NativeFunction and an add.into_js_function_copied() when passing that to register_global_property. I think we could make the interface of register_global_property better for functions, but overall this should work with minimal boilerplate with the boa_interop crate.

from boa.

hansl avatar hansl commented on May 1, 2024

Oh the PR is not in yet; #3773

from boa.

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.