Pure
Pure 是我写的一个轻量级的 JavaScript runtime, 写的动机是最近先后出现了 Next.js Edge Runtime、cloudflare workerd、Noslate JavaScript worker 等 3 个相似的产品, 所以我想通过写 Pure 与写完后它的性能表现来感受为何大家都会投入进来 🤔
由于本人 C++ 段位仅为初级 👷, 所以记录一下写 Pure 遇见的一些坑与解决的办法
常见问题
v8 编译问题见: MacBook M1 编译 v8 问题记录
Embedder-vs-V8 build configuration mismatch.
1 | # Fatal error in , line 0 |
问题解决: 编译参数加上 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX
1 | g++ src/pure.cc -g -I deps/v8/include/ -o pure -L lib/ -lv8_monolith -pthread -std=c++17 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX |
FailureMessage Object: 0x16fdfed98Process 97650 stopped
1 | # |
问题解决:
- 运行如下命令使用lldb 开始调试将会打印如下日志
1
lldb -- ./pure
1
2
3
4
5
6
7
8* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00000001877799b8 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
-> 0x1877799b8 <+8>: b.lo 0x1877799d8 ; <+40>
0x1877799bc <+12>: pacibsp
0x1877799c0 <+16>: stp x29, x30, [sp, #-0x10]!
0x1877799c4 <+20>: mov x29, sp
Target 0: (pure) stopped. - 运行如下命令显示当前线程的堆栈回溯将会打印如下日志, 即可找到源码开始报错的地方进行修复
1
thread backtrace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
* frame #0: 0x00000001877799b8 libsystem_kernel.dylib`__pthread_kill + 8
frame #1: 0x00000001877aceb0 libsystem_pthread.dylib`pthread_kill + 288
frame #2: 0x00000001876ea314 libsystem_c.dylib`abort + 164
frame #3: 0x00000001000250b0 pure`v8::base::OS::Abort() at platform-posix.cc:677:3 [opt]
frame #4: 0x000000010001aa30 pure`V8_Fatal(format=<unavailable>) at logging.cc:167:3 [opt]
frame #5: 0x000000010037fce4 pure`v8::internal::IsolateAllocator::IsolateAllocator(this=0x0000000102704250) at isolate-allocator.cc:0 [opt]
frame #6: 0x00000001001f92c4 pure`v8::internal::Isolate::New() [inlined] std::__1::__unique_if<v8::internal::IsolateAllocator>::__unique_single std::__1::make_unique<v8::internal::IsolateAllocator>() at unique_ptr.h:728:32 [opt]
frame #7: 0x00000001001f92b8 pure`v8::internal::Isolate::New() [inlined] v8::internal::Isolate::Allocate(is_shared=false) at isolate.cc:3353:7 [opt]
frame #8: 0x00000001001f92b8 pure`v8::internal::Isolate::New() at isolate.cc:3335:22 [opt]
frame #9: 0x000000010000b380 pure`pure::PureMainInstance::PureMainInstance(this=0x000000016fdff128, event_loop=0x0000000101236858, args=size=0, exec_args=size=0) at pure_main_instance.cc:90:16
frame #10: 0x000000010000b574 pure`pure::PureMainInstance::PureMainInstance(this=0x000000016fdff128, event_loop=0x0000000101236858, args=size=0, exec_args=size=0) at pure_main_instance.cc:88:3
frame #11: 0x00000001000080e0 pure`pure::Start(argc=1, argv=0x000000016fdff398) at pure.cc:80:26
frame #12: 0x000000010000b164 pure`main(argc=1, argv=0x000000016fdff398) at pure_main.cc:10:12
frame #13: 0x00000001023050f4 dyld`start + 520
Undefined symbols for architecture arm64
1 | Undefined symbols for architecture arm64: |
问题解决: 通常是头文件中定义了类型, 却没有具体实现。找到未实现的定义, 在 .cc 或者 .inl.h 中实现即可。