-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSFunction.h
More file actions
86 lines (77 loc) · 2.22 KB
/
JSFunction.h
File metadata and controls
86 lines (77 loc) · 2.22 KB
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
#ifndef JS_FUNCTION_H
#define JS_FUNCTION_H
#include "JSValue.h"
#include "GCObject.h"
struct IStmtNode;
typedef shared_ptr<IStmtNode> StmtNodePtr;
struct Function:
public GCObject {
enum FuncType {
FT_JS,
FT_C,
};
FuncType funcType;
void destroy();
void accessGCObjects(vector<GCObject*> &objs);
JSValue callFromC(JSValue* argsBegin, JSValue* argsEnd);
void callFromVM(JSValue *argsBegin, JSValue* argsEnd);
protected:
Function(FuncType _funcType): GCObject(GCT_Function), funcType(_funcType){
GCObjectManager::instance()->link(this);
}
};
struct FuncMeta {
string fileName;
int argCount;
int localCount, tempCount;
vector<int> codes;
vector<int> ip2line;
StmtNodePtr stmt;
vector<JSValue> constTable;
FuncMeta(const string &_fileName): fileName(_fileName), argCount(0), localCount(0), tempCount(0){}
int getLocalSpace() const { return localCount + tempCount; }
int getConstIdx(const JSValue& cv) {
for (int i = 0; i < (int)constTable.size(); ++i) {
if (cv == constTable[i]) return i;
}
constTable.push_back(cv);
return (int)constTable.size() - 1;
}
void accessGCObjects(vector<GCObject*> &objs) {
for (auto &v : constTable) {
if (auto obj = v.gcAccess()) objs.push_back(obj);
}
}
};
typedef shared_ptr<FuncMeta> FuncMetaPtr;
struct JSFunction:
public Function {
FuncMetaPtr meta;
static JSFunction* create(const FuncMetaPtr& meta) {
return new JSFunction(meta);
}
private:
JSFunction(const FuncMetaPtr& _meta): Function(FT_JS), meta(_meta){}
};
typedef int (*CFuncT)(JSValue* argsBegin, JSValue* argsEnd);
struct CFuncEntry {
const char *name;
CFuncT func;
};
struct CFunction:
public Function {
CFuncT func;
static CFunction* create(CFuncT func) {
return new CFunction(func);
}
private:
CFunction(CFuncT _func): Function(FT_C), func(_func){}
};
inline void Function::destroy() {
switch (funcType) {
case Function::FT_C: delete static_cast<CFunction*>(this); break;
case Function::FT_JS: delete static_cast<JSFunction*>(this); break;
default: ASSERT(0); break;
}
}
#endif