Comments (2)
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.
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)
- Implement symbol type
- Implement typed arrays
- Implement data views
- Implement external properties data
- Cloning Context/Value fails w/o ContextGuard HOT 2
- Having trouble building on macOS HOT 8
- Can't build to target musl on Ubuntu HOT 2
- lib directory does not exist on Windows custom
- JsCopyString expected 5 parameters HOT 5
- How to load a js file? HOT 3
- access violation with call_with_this HOT 9
- Compilation problem HOT 3
- Segfault when dropping Context
- Only one thread can access the same context at a given time HOT 15
- Audit context guard soundness HOT 2
- Build from binaries? HOT 2
- 0.2.0 does not build HOT 1
- Value::to_json should return Result HOT 1
- Exception source, line, and column info
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chakracore-rs.