Comments (3)
You need to read the file contents using standard IO.
I haven't tried to compile it but it should be something along these lines:
extern crate charakcore as js;
use std::io::prelude::*;
use std::fs::File;
fn main() {
let runtime = js::Runtime::new().unwrap();
let context = js::Context::new(&runtime).unwrap();
let guard = context.make_current().unwrap();
let mut file = File::open("typescript.js").expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read the file");
let result = js::script::eval(&guard, &contents).expect("Invalid JavaScript code");
let ts = result.into_object().expect("Script did not return an object");
let version = ts.get(&guard, &Property::new(&guard, "version")).to_string(&guard);
assert_eq!(&version, "2.4.2");
}
from chakracore-rs.
It also worked to get the ts
object from global:
extern crate chakracore as js;
use std::io::prelude::*;
use std::fs::File;
use js::Property;
fn main() {
let runtime = js::Runtime::new().unwrap();
let context = js::Context::new(&runtime).unwrap();
let guard = context.make_current().unwrap();
let js = r"C:\Users\camer\ts\TsAst\node_modules\typescript\lib\typescript.js";
let mut file = File::open(js).expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read the file");
js::script::eval(&guard, &contents).expect("Invalid JavaScript code");
let ts = guard.global().get(&guard, &Property::new(&guard, "ts")).into_object().unwrap();
let version = ts.get(&guard, &Property::new(&guard, "version")).to_string(&guard);
println!("version: {:?}", version);
}
from chakracore-rs.
Thank you so much @darfink. I worked on this yesterday and got it working. Here was one solution:
extern crate chakracore as js;
use std::io::prelude::*;
use std::fs::File;
fn main() {
let runtime = js::Runtime::new().unwrap();
let context = js::Context::new(&runtime).unwrap();
let guard = context.make_current().unwrap();
let js = r"C:\Users\camer\ts\TsAst\node_modules\typescript\lib\typescript.js";
let mut file = File::open(js).expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Unable to read the file");
js::script::eval(&guard, &contents).expect("Invalid JavaScript code");
let v = js::script::eval(&guard, "ts.version").unwrap().to_string(&guard);
println!("version: {:?}", v);
}
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 pass function callbacks? HOT 2
- 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.