Skip to content

Commit

Permalink
faster hrtime
Browse files Browse the repository at this point in the history
  • Loading branch information
billywhizz committed Aug 11, 2022
1 parent ee760a1 commit c598dc9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions just.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ void just::builtins_add (const char* name, const char* source,
void just::SET_METHOD(Isolate *isolate, Local<ObjectTemplate>
recv, const char *name, FunctionCallback callback) {
recv->Set(String::NewFromUtf8(isolate, name,
NewStringType::kNormal).ToLocalChecked(),
NewStringType::kInternalized).ToLocalChecked(),
FunctionTemplate::New(isolate, callback));
}

void just::SET_MODULE(Isolate *isolate, Local<ObjectTemplate>
recv, const char *name, Local<ObjectTemplate> module) {
recv->Set(String::NewFromUtf8(isolate, name,
NewStringType::kNormal).ToLocalChecked(),
NewStringType::kInternalized).ToLocalChecked(),
module);
}

void just::SET_VALUE(Isolate *isolate, Local<ObjectTemplate>
recv, const char *name, Local<Value> value) {
recv->Set(String::NewFromUtf8(isolate, name,
NewStringType::kNormal).ToLocalChecked(),
NewStringType::kInternalized).ToLocalChecked(),
value);
}

Expand Down Expand Up @@ -254,7 +254,6 @@ v8::MaybeLocal<v8::Promise> OnDynamicImport(v8::Local<v8::Context> context,
v8::Local<v8::ScriptOrModule> referrer,
v8::Local<v8::String> specifier,
v8::Local<v8::FixedArray> import_assertions) {
fprintf(stderr, "OnDynamicImport\n");
v8::Local<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(context).ToLocalChecked();
v8::MaybeLocal<v8::Promise> promise(resolver->GetPromise());
Expand Down Expand Up @@ -559,7 +558,8 @@ void just::Modules(const FunctionCallbackInfo<Value> &args) {
}

void just::HRTime(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(BigInt::New(args.GetIsolate(), just::hrtime()));
uint64_t* ptr = reinterpret_cast<uint64_t*>(Local<BigInt>::Cast(args[0])->Uint64Value());
*ptr = just::hrtime();
}

void just::Init(Isolate* isolate, Local<ObjectTemplate> target) {
Expand Down
1 change: 1 addition & 0 deletions just.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <v8-fast-api-calls.h>

namespace just {

Expand Down
11 changes: 11 additions & 0 deletions just.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
(function () {
function wrapHRTime (hrtime) {
const buf = new ArrayBuffer(8)
const u64 = new BigUint64Array(buf)
const address = buf.getAddress()
return () => {
hrtime(address)
return u64[0]
}
}

function wrapMemoryUsage (memoryUsage) {
const mem = new BigUint64Array(16)
return () => {
Expand Down Expand Up @@ -289,6 +299,7 @@
just.net.setNonBlocking = setNonBlocking
just.require = global.require = require
just.require.cache = cache
just.hrtime = wrapHRTime(just.hrtime)
just.memoryUsage = wrapMemoryUsage(just.memoryUsage)
just.cpuUsage = wrapCpuUsage(just.sys.cpuUsage)
just.rUsage = wrapgetrUsage(just.sys.getrUsage)
Expand Down

0 comments on commit c598dc9

Please sign in to comment.