iotjs icon indicating copy to clipboard operation
iotjs copied to clipboard

Implement native JS(src/js/*.js) modules as c builtin code

Open chunseoklee opened this issue 11 years ago • 7 comments

We have to discuss the following topics.

  1. Benifit
    • size
    • performance
  2. Feasibility

chunseoklee avatar Jun 19 '15 01:06 chunseoklee

function foo() {
  do something
}

IMHO, replacing do something part with c builtin function is only thing we can do now. Q. how to replace the whole foo function with c builtin not affecting module system ?

chunseoklee avatar Jun 19 '15 01:06 chunseoklee

What if we can implement not by level of function() but by functionality, such as file, socket, server, can it be possible?

seanshpark avatar Jun 19 '15 05:06 seanshpark

It's possible to create builtin object and add properties with native handlers.

egavrin avatar Jun 19 '15 05:06 egavrin

Like @seanshpark suggests it will look like:

File.open ("aaa") // open is native handler

egavrin avatar Jun 19 '15 05:06 egavrin

simple function() implementation as built-in is ok. as iotjs modules uses exports to export functions, it will be different.

function foo() {
  do something
}
exports.foo = foo;

seanshpark avatar Jun 19 '15 05:06 seanshpark

We can do that.

JHandler(Foo, handler) {
// do somthing
}
...
Module* module = GetBuiltinModule(SOME_MODULE);
module->module = new JObject();
module->module->SetMethod("foo", Foo);
var some_module = process.binding(process.binding.some_module);

exports.foo = some_module.foo;

ILyoan avatar Jun 19 '15 05:06 ILyoan

Closing.

C code will be harder to read, write, and maintain, etc. It can decrease development productivity, especially for current IoT.js whose milestone 1 has not been finished yet. And it can be a bigger problem for developers who are familliar with node.js and v8 APIs, but not IoT.js and JerryScript APIs. They may even want to refer to existing codes when their new module implemetation, but it will be harder to do if everything is written in C.

On the other hand, it is not straightforward how much benefit we can achieve. For example, when reading a named property of JS object (simple obj.prop), to perfectly describe the behavior of JS equivalence in C, we should execute complex operations like: catch and throw proper error when property getter throws, sometimes check if property exists before actually reading it, etc.

So I think it's better to open a new issue when a specific module really needs a re-implementation. (like PR #393)

yichoi avatar Oct 04 '16 04:10 yichoi