GithubHelp home page GithubHelp logo

anyouzy / node-c-jit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zy445566/node-c-jit

0.0 1.0 0.0 17 KB

node.JS run C language Just In Time

License: MIT License

JavaScript 93.90% Python 1.27% C++ 4.83%

node-c-jit's Introduction

node-c-jit

Build Status
node.JS run C language Just In Time

install


npm install c-jit

prepare


npm install node-gyp -g

You can see:https://github.com/nodejs/node-gyp#installation

example


Grammar reference(NAN example):https://github.com/nodejs/node-addon-examples

sync:

const CJit = require("c-jit");
const path = require("path");
let cJit  = new CJit();

// run by c code sync
let funcByrunSync = cJit.runSync(`
  if (info.Length() < 2) {
    Nan::ThrowTypeError("Wrong number of arguments");
    return;
  }

  if (!info[0]->IsNumber() || !info[1]->IsNumber()) {
    Nan::ThrowTypeError("Wrong arguments");
    return;
  }

  double arg0 = info[0]->NumberValue();
  double arg1 = info[1]->NumberValue();
  v8::Local<v8::Number> num = Nan::New(arg0 + arg1);

  info.GetReturnValue().Set(num);
`);
console.log("This should be eight(by run sync):"+funcByrunSync(3,5));

//run by file sync
let funcByfileSync = cJit.runByFileSync(path.join(__dirname,'test.cc'));
console.log("This should be twelve(by file sync):"+funcByfileSync(6,6));

async:

const CJit = require("c-jit");
const path = require("path");
let cJit  = new CJit();

let funcByrun = cJit.run(`
  if (info.Length() < 2) { 
    Nan::ThrowTypeError("Wrong number of arguments"); 
    return; 
  } 
 
  if (!info[0]->IsNumber() || !info[1]->IsNumber()) { 
    Nan::ThrowTypeError("Wrong arguments"); 
    return; 
  } 
 
  double arg0 = info[0]->NumberValue(); 
  double arg1 = info[1]->NumberValue(); 
  v8::Local<v8::Number> num = Nan::New(arg0 + arg1); 
 
  info.GetReturnValue().Set(num); 
`,(err,func)=>{
  if (err){console.log(err);return;}
  console.log("This should be eight(by run):"+func(3,5));
});


// run by file
let funcByfile = cJit.runByFile(path.join(__dirname,'test.cc'),(err,func)=>{
  if (err){console.log(err);return;}
  console.log("This should be twelve(by file):"+func(6,6));
});

node-c-jit's People

Contributors

zy445566 avatar

Watchers

 avatar

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.