V8笔记
18 Sep 2016 | categories notesv8::internal::String
length() -> int
ToAsciiArray() -> char*
CompilationInfo
parse_info() -> ParseInfo
ParseInfo
literal() -> FunctionLiteral
script() -> Script
Script
source() -> String(代码)
CompileToplevel
// 编译代码
Parser::ParseProgram
// 解析语法
CompileUnoptimizedCode
// JIT编译
Compiler::Analyze
Rewriter::Rewrite
Scope::Analyze
Renumber
FullCodeGenerator::MakeCode
代码生成在 src/full-codegen/x64/full-codegen-x64.cc
b v8::internal::Processor::VisitNoStackOverflowCheck # Rewriter::Rewrite 的实现内部
b ../../src/compiler.cc:376
b v8::internal::(anonymous namespace)::GenerateUnoptimizedCode # 代码生成入口
b interpreter::Interpreter::NewCompilationJob # 解释器代码实现
b FullCodeGenerator::NewCompilationJob # JIT代码生成
b v8::internal::FullCodeGenerator::Generate 汇编代码生成过程
Execution::Call
// 运行代码
--trace_regexp_bytecodes
// 打印正则处理过程
Runtime_RegExpInitializeAndCompile
// 正则编译
RegExpImpl::Compile
RegExpParser::ParseRegExp
parser.ParsePattern
RegExpParser::ParseDisjunction
// 语法解析过程
// 查看正则语法树 parse_result->tree
CompileIrregexp
Runtime_RegExpExec
// 正则执行
AtomExec
// RegExp Atom implementation: Simple string search using indexOf.
IrregexpExec
// 正则匹配过程
RegExpImpl::CompileIrregexp
//编译成字节码
RegExpEngine::Compile
RegExpCompiler::Assemble
//编译实现
RawMatch
// 匹配
https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/regexp/jsregexp.cc&sq=package:chromium&type=cs&l=707&rcl=1445874174
// 正则实现说明
https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/regexp/interpreter-irregexp.cc&sq=package:chromium&type=cs&l=176&rcl=1445945869
正则解释器匹配过程
https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/regexp/bytecodes-irregexp.h&sq=package:chromium&type=cs&l=20&rcl=1445945869
字节码定义
字节码编译条件:
FLAG_ignition
TopLevelFunctionPassesFilter(FLAG_ignition_filter)
ScriptPassesFilter(FLAG_ignition_script_filter, script)
AddClass
// 添加一个范围
AddClassNegated
// 添加给定的范围之外的范围
换行: \x0a, \x0d, \u2028 and \u20f29
kMaxInt 0x7FFFFFFF,2GB
/x??x/.exec("xx")
// ["x"]
//第二个 ? 标志贪婪匹配
/x? ?x/.exec("xx")
// ["xx"]
/( (a) | (bc) ) ? (a) ( (de) | (bcde) )/.exec("abcde")
// ["abcde", undefined, undefined, undefined, "a", "bcde", undefined, "bcde"]
/[^]+/.exec("bxxx\nx")
// ["bxxx\nx"]
匹配过程:
-
字符匹配
- . 匹配非 \x0a \x0d \u2028 \u2029 字符
- (?: GROUPING
- (?= POSITIVE_LOOKAHEA
- (?! NEGATIVE_LOOKAHEAD
测试正则:
-
正则:
/a.*(f)[g1-9]{2,6}l?\1/.test("abf333ff")
-
语法树:
char a atom RegExpCharacterClass . AddQuantifierToAtom * captures_Add char f captures_End
-
编译(4 byte):
\x02 \x00
s