Skip to content

Instantly share code, notes, and snippets.

@pkit
Last active October 8, 2020 17:19
Show Gist options
  • Save pkit/427d3ac691882782c61e6ceec490348c to your computer and use it in GitHub Desktop.
Save pkit/427d3ac691882782c61e6ceec490348c to your computer and use it in GitHub Desktop.
v8 BigInt parser test
CC = g++
CFLAGS = -Wall -std=c++11 -fno-rtti -O2 -DV8_COMPRESS_POINTERS -DV8_31BIT_SMIS_ON_64BIT_ARCH -I../include -I.. -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -fPIC -c
LDFLAGS = -Wall -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O2 -fstack-protector-strong -fno-omit-frame-pointer
V8_OUT ?= ../out.gn/x64.release/obj
ZLIB_OUT = $(V8_OUT)/third_party/zlib
ZLIB_OBJS = $(ZLIB_OUT)/zlib_adler32_simd/adler32_simd.o $(ZLIB_OUT)/zlib_inflate_chunk_simd/inffast_chunk.o $(ZLIB_OUT)/zlib_inflate_chunk_simd/inflate.o $(ZLIB_OUT)/zlib_crc32_simd/crc32_simd.o $(ZLIB_OUT)/zlib_x86_simd/crc_folding.o $(ZLIB_OUT)/zlib_x86_simd/fill_window_sse.o
V8_LIBS = -lv8_base_without_compiler -lv8_compiler -lv8_base_without_compiler -lchrome_zlib -lcompression_utils_portable -ltorque_generated_definitions -lcppgc_base -lv8_cppgc_shared -lv8_snapshot -lv8_libplatform -lv8_libbase -lv8_libsampler -lchrome_zlib -lv8_libplatform
LIBS = -L/usr/lib/x86_64-linux-gnu -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed $(ZLIB_OBJS) -L$(V8_OUT) -L$(V8_OUT)/third_party/icu -L$(ZLIB_OUT) -L$(ZLIB_OUT)/google $(V8_LIBS) -lrt -lc++ -lpthread
SOURCES = test1.cc
OBJECTS = $(SOURCES:.cc=.o)
EXECUTABLE = test1
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBS)
.cc.o:
$(CC) $(CFLAGS) $< -o $@
clean:
rm -fr $(EXECUTABLE) $(OBJECTS)
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
const double timeout = 0.5;
void * Breakout (void *d)
{
auto *isolate = (v8::Isolate *) d;
struct timespec tp;
tp.tv_sec = (long) timeout;
tp.tv_nsec = (timeout - tp.tv_sec) * 1000000000L;
nanosleep(&tp, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
isolate->TerminateExecution();
return NULL;
}
void print_elapsed (timespec start, timespec end) {
timespec diff;
if ((end.tv_nsec-start.tv_nsec)<0) {
diff.tv_sec = end.tv_sec - start.tv_sec - 1;
diff.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
} else {
diff.tv_sec = end.tv_sec - start.tv_sec;
diff.tv_nsec = end.tv_nsec - start.tv_nsec;
}
printf("elapsed: %lld.%.3lds\n", (long long)diff.tv_sec, diff.tv_nsec / 1000000L);
}
int main(int argc, char* argv[]) {
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope context_scope(context);
{
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, argv[1]).ToLocalChecked();
printf("timeout = %.3fs\n", timeout);
pthread_t breakout_thread;
pthread_create(&breakout_thread, NULL, Breakout, isolate);
timespec time1, time2;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
v8::Local<v8::Script> script;
v8::Local<v8::Value> result;
if (v8::Script::Compile(context, source).ToLocal(&script)) {
if (script->Run(context).ToLocal(&result)) {
v8::String::Utf8Value utf8(isolate, result);
printf("%s\n", *utf8);
}
}
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time2);
void *thread_result;
pthread_cancel(breakout_thread);
pthread_join(breakout_thread, &thread_result);
if (thread_result == NULL) {
printf("timed out\n");
print_elapsed(time1, time2);
}
}
}
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment