Giter Club home page Giter Club logo

Comments (2)

darfink avatar darfink commented on May 27, 2024

You are calling the filter method without any context (i.e this is not set to an instance).
Which roughly corresponds to this javascript:

let array = [];
for (var i = 0; i < 10; i++) {
  array.push(i);
}
let is_even = (x) => x % 2 == 0;
let fn_filter = array.filter;
fn_filter(is_even);

Therefore your solution is to call fn_filter with the array instance:

let filtered_array = fn_filter.call(&guard, &[&is_even]).unwrap();
// --->
let filtered_array = fn_filter.call_with_this(&guard, &array, &[&is_even]).unwrap();

from chakracore-rs.

ctaggart avatar ctaggart commented on May 27, 2024

Thank you for the clear explanation. It works great.

Cleaned up & works:

extern crate chakracore as js;

use js::Property;

fn main() {
    let runtime = js::Runtime::new().unwrap();
    let context = js::Context::new(&runtime).unwrap();
    let guard = context.make_current().unwrap();

    // create a JavaScript Array
    let length = 10;
    let array = js::value::Array::new(&guard, length);
    for i in 0..length {
        array.set_index(&guard, i, &js::value::Number::new(&guard, i as i32));
    }

    // try filtering it
    let is_even = js::value::Function::new(&guard, Box::new(|guard, info| {
        let i = info.arguments[0].to_integer(guard);
        Ok(js::value::Boolean::new(guard, i % 2 == 0).into())
    }));

    let fn_filter = array.get(&guard, &Property::new(&guard, "filter")).into_function().unwrap();

    let filtered_array = fn_filter.call_with_this(&guard, &array, &[&is_even]);
    println!("filtered_array: {:?}", filtered_array);
}

prints as expected:

filtered_array: Ok(Value(Array: 0,2,4,6,8))

from chakracore-rs.

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.