Giter Club home page Giter Club logo

Comments (3)

darfink avatar darfink commented on May 28, 2024 1

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.

ctaggart avatar ctaggart commented on May 28, 2024 1

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.

ctaggart avatar ctaggart commented on May 28, 2024

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)

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.