javascript - V8: console.log implementation -
i'm using v8 in c++ app , add console.log()
. there standard implementation can use?
currently, have own dummy implementation it's quite incomplete.
i found implementation node.js:
https://github.com/joyent/node/blob/master/lib/console.js https://github.com/joyent/node/blob/master/lib/util.js
of course need adopt code. uses nodejs global object process
, stdout
, stderr
objects. here sample c++ code create such file objects:
static void js_file_write(const v8::functioncallbackinfo<v8::value>& info) { int fd = (int) info.data().as<external>()->value(); auto isolate = isolate::getcurrent(); if( info.length() != 1) { isolate->throwexception( v8::exception::typeerror(jsstr("js_file_write: expects 1 arg"))); return; } if( !info[0]->isstring() ) { isolate->throwexception( v8::exception::typeerror(jsstr("js_file_write: expects string"))); return; } std::string s = jsobjtostring(info[0], false, ""); size_t c = 0; while(c < s.size()) { int ret = write(fd, &s[c], (unsigned int) (s.size() - c)); if(ret < 0) { isolate->throwexception( v8::exception::error(jsstr("js_file_write: write(" + to_string(fd) + ") error " + to_string(errno) + ": " + strerror(errno)))); errno = 0; return; } c += ret; } } static local<object> makefileobj(int fd) { auto isolate = isolate::getcurrent(); handle<external> selfref = external::new(isolate, (void*) fd); auto obj = object::new(isolate); obj->set(jsstr("write"), functiontemplate::new(isolate, js_file_write, selfref)->getfunction()); return obj; } static local<object> makeprocessobj() { auto obj = object::new(isolate::getcurrent()); obj->set(jsstr("stdout"), makefileobj(stdout_fileno)); obj->set(jsstr("stderr"), makefileobj(stderr_fileno)); return obj; }
jsstr
, jsobjtostring
quite canonical.
Comments
Post a Comment