永远的魔术1号
一名小小的软件工程师
展开
-
一、编译v8引擎
官方网站What is V8?V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others.1. 源码仓库V8’s Git repository is located athttps://chromium.googlesource....原创 2021-06-27 14:25:19 · 553 阅读 · 0 评论 -
二、执行v8引擎示例代码
文章里使用v8源代码的目录下的示例代码v8\samples\hello-world.cc// Copyright 2015 the V8 project authors. All rights reserved.// Use of this source code is governed by a BSD-style license that can be// found in the LICENSE file.#include <stdio.h>#inclu...原创 2021-06-27 23:55:34 · 916 阅读 · 2 评论 -
三、VS2019编译v8引擎
接着上一篇实践发现的问题《C++使用v8引擎执行JavaScript_永远的魔术1号-优快云博客》,我在网上查找了很多资料,原因都是说,由于v8默认使用了clang编译,而clang使用的是libc++作为C++标准库,而demo使用的环境则是使用libstdc++作为标准库,两种标准库不同,存在inline namespace __1的问题。 然后又看到网上又很多提到可以使用gn命令编译,在命令中增加参数控制不使用clang,于是我尝试着编译了一下。先说一下结果,编译过...原创 2021-07-01 22:05:19 · 429 阅读 · 3 评论 -
四、Node.js中的v8
由于之前编译v8默认使用clang.exe,导致编译出的库文件函数都带有__1命名空间,与cl编译的程序无法配合。尝试使用msvc的cl.exe编译(编译选项is_clang=false),却又在Link v8的时候报出了一堆乱码的错误,也不知道如何解决。所以换个思路,尝试编译Node.js,从编译出的文件中得到v8相关的库文件。一、Node.js简介Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时。二、下载源码 ...原创 2021-07-26 23:06:35 · 693 阅读 · 1 评论 -
五、再次执行hello-world.cc
继续执行示例《二、执行v8引擎示例代码_永远的魔术1号-优快云博客》添加 include到项目“附加包含目录”; 添加项目“附加库目录”; 添加 node\Debug\lib下所有库文件到项目“附加依赖项”。这里是库文件的文件名列表:brotli.libcares.libhistogram.libicudata.libicui18n.libicutools.libicuucx.liblibnode.liblibuv.libllhttp...原创 2021-07-27 21:40:59 · 217 阅读 · 0 评论 -
六、v8引擎执行JS文件
这个环节比较简单,直接上代码了main.cpp// Copyright 2015 the V8 project authors. All rights reserved.// Use of this source code is governed by a BSD-style license that can be// found in the LICENSE file.#include <stdio.h>#include <stdlib.h>#...原创 2021-07-27 22:05:18 · 717 阅读 · 0 评论 -
七、嵌入v8引擎访问全局变量
参考《Getting started with embedding V8 · V8》Accessing static global variables首先,封装对全局变量的Set和Get方法:void GlobalValueGetter( Local<String> property, const PropertyCallbackInfo<Value>& info ) { info.GetReturnValue().Set( gValue...原创 2021-07-27 22:51:49 · 388 阅读 · 1 评论 -
八、JS使用C++类及其方法
参考《Getting started with embedding V8 · V8》Accessing dynamic variables首先,创建Point类class Point{public: Point( int x, int y ) : x_( x ), y_( y ) { } int x_, y_;};其次,编写Point类的创建函数:// point constructorvoid PointConstructor( const ...原创 2021-08-16 23:16:51 · 312 阅读 · 0 评论 -
九、JS使用C++对象及其属性方法
参考《Getting started with embedding V8 · V8》Accessing dynamic variables首先,创建Point类#pragma onceclass Point{public: Point( int x, int y ) : x_( x ), y_( y ) { } int x_, y_; int Add() { return x_ + y_; }};定义访问x_,y_的方法:// ge...原创 2021-08-17 23:33:40 · 262 阅读 · 0 评论 -
十、JS调用C++函数
参考《Getting started with embedding V8 · V8》Templates首先,C++函数,这里以文档中的 LogCallback 为例:void LogCallback( const v8::FunctionCallbackInfo<v8::Value>& info ) { if ( info.Length() < 1 ) return; v8::Isolate* isolate = info.GetIs...原创 2021-08-18 22:10:36 · 2753 阅读 · 0 评论 -
十一、JS调用C++函数附带外部数据
有时候我们需要对不同的js函数名称设置相同的回调函数,如何实现呢?场景:需要通过注入C++函数为js提供分级日志函数。重点函数: static Local<FunctionTemplate> New( Isolate* isolate, FunctionCallback callback = nullptr, Local<Value> data = Local<Value>(), Local<Sig...原创 2021-09-02 23:04:52 · 278 阅读 · 0 评论 -
十二、JS调用C++函数抛出异常及捕捉异常
本文讲述如何利用v8::TryCatch捕捉js代码中发生的异常。首先,声明TryCatch对象。v8::TryCatch trycatch( isolate );然后,定义抛出异常的函数:void ThrowException( const v8::FunctionCallbackInfo<v8::Value>& info ) { v8::Isolate* isolate = info.GetIsolate(); v8::Handle...原创 2021-08-18 23:41:37 · 322 阅读 · 0 评论 -
十三、v8 platform里thread pool使用
在v8源码 default-platform.h 中,DefaultPlatform类中有一个私有对象worker_threads_task_runner_,是一个线程池对象,这里研究一下如何使用这个对象执行多个任务。首先,参照hello-world.cc示例中的代码,因为isolate的需要和脚本执行在同一线程中,所以从isolate创建部分开始直至脚本执行完毕全部封装为一个函数。内容不复杂,直接上源码吧。main.cpp// Copyright 2015 the V...原创 2021-08-20 00:06:19 · 352 阅读 · 0 评论 -
十四、再次使用msvc编译v8静态库
经验证,可通过以下命令编译出 v8_monolith.lib release版本且可以使用,但试图编译debug版本时依旧报错,没有解决。python .\tools\dev\v8gen.py x64.release -- v8_monolithic=true v8_use_external_startup_data=false use_custom_libcxx=false is_component_build=false treat_warnings_as_errors=false...原创 2021-08-21 23:52:03 · 696 阅读 · 0 评论