-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (89 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
var fantasy = require('ramda-fantasy')
var Maybe = fantasy.Maybe
var Either = fantasy.Either
var Left = Either.Left
var Right = Either.Right
var INLINED = /^Inlined\s(?:(get|set)\s)?([^\s]+)\s(?:(tail)\s)?called from\s(?:(get|set)\s)?([^.\s]+)?/
var INLINED_NATIVE = /^Inlining\s(api|builtin)(?:\sfunction)?\s([^\s]+)\s<JS Function\s(?:(get|set)\s)?([^(\s]+)?/
var NOT_INLINED = /^Did not inline\s(?:(get|set)\s)?([^\s]+)\scalled from\s(?:(get|set)\s)?([^\s]+)?\s\(([^).]+)\)/
var TYPE_JS = 'func'
var TYPE_NATIVE = 'native'
module.exports = parse
/**
* main parser
* @param line {string}
*/
function parse(line) {
assert_str(line)
return Either.either(pipe, noop, Right(line).chain(is_inlined).chain(is_native).chain(is_not_inlined))
}
// helper
function pipe(q) {
return q
}
// helper
function noop() {}
function is_inlined(line) {
return match_line(line, INLINED).map(build_inlined).getOrElse(Right(line))
}
function is_native(line) {
return match_line(line, INLINED_NATIVE).map(build_native).getOrElse(Right(line))
}
function is_not_inlined(line) {
return match_line(line, NOT_INLINED).map(build_not_inlined).getOrElse(Right(line))
}
/**
* check to string type
*/
function assert_str(s) {
if (typeof s !== 'string') {
throw new TypeError('expected string')
}
}
/**
* test string to regexp and return Maybe monad
*/
function match_line(line, rx) {
return Maybe(line.match(rx))
}
/**
* create `target` or `caller` nodes
*/
function node(name, accessor) {
if (!name) {
return false
}
return {
name: name,
accessor: accessor ? accessor : false
}
}
function build_inlined(match) {
return Left({
target: node(match[2], match[1]),
caller: node(match[5], match[4]),
type: TYPE_JS,
inlined: true,
tailcall: !!match[3]
})
}
function build_not_inlined(match) {
return Left({
target: node(match[2], match[1]),
caller: node(match[4], match[3]),
type: TYPE_JS,
inlined: false,
reason: match[5]
})
}
function build_native(match) {
return Left({
target: node(match[4], match[3]),
caller: false,
type: TYPE_NATIVE,
inlined: true,
place: match[1],
address: match[2]
})
}