-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.h
65 lines (62 loc) · 2.15 KB
/
js.h
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
//-----本文件是关于maincall类中JS操作的实现-----
//-----本文件是游戏中的一些函数调用的实现,引擎的三层封装-----
#pragma once
#include <QString>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValue>
#include <QVariant>
#include "tableManager.h"
#include "aggHelper.h"
typedef QScriptEngine JSVM;
typedef QString String;
typedef QVariant Variant;
#define NULL_String ""
static JSVM* AddJSVM()
{
JSVM *VM=new JSVM;
QScriptValue tablemanager=VM->newQObject(tableManager::tablemanager);
VM->globalObject().setProperty("tablemanager",tablemanager);
QScriptValue agghelper=VM->newQObject(aggHelper::agghelper);
VM->globalObject().setProperty("agghelper",agghelper);
QScriptValue typehelper=VM->newQObject(typeHelper::typehelper);
VM->globalObject().setProperty("typehelper",typehelper);
qScriptRegisterSequenceMetaType<QVector<QString> >(VM);
return VM;
}
static Variant JSEval(String code,String functionname, String *mistake,JSVM *VM)
{
QScriptValue ret;
if(code!=NULL_String)
{ret=VM->evaluate(code);}
if(functionname!=NULL_String)
{
ret=VM->globalObject().property(functionname);
ret=ret.call(QScriptValue());
}
if(ret.isError())
{
if(mistake!=nullptr)
*mistake=String::number(ret.property("lineNumber").toInt32())+" line:"+ret.toString();
return NULL;
}
return ret.toVariant();
}
/*
void JSSendPar(ParametersStru *Parame,String ParameName,JSVM *VM) //注意,使用指针仅因迫不得已,实际创建请不要new
{
QScriptValue para=VM->newQObject(Parame);
VM->globalObject().setProperty(ParameName,para);
}
void JSSendJSPar(JSParStru Parame,JSVM *VM)
{
for(int i=0;i<Parame.pointerVec.length();i++)
{
QScriptValue para=VM->newQObject(Parame.pointerVec[i]);
VM->globalObject().setProperty(Parame.nameVec[i],para);
}
}
Variant JSEvalFile(String path,String functionname,String *mistake,JSVM *VM)
{return JSEval(ReadTXT(path),functionname,mistake,VM);}
*/
static Variant JSCallFun(String functionname,String *mistake,JSVM *VM)
{return JSEval(NULL_String,functionname,mistake,VM);}