File: objtest.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (162 lines) | stat: -rw-r--r-- 3,496 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <ClanLib/javascript.h>
#include <iostream>

class MemberObject : public CL_JSObject
{
public:
	MemberObject(CL_JSContext &context)
	: x(10), y(20)
	{
		newObject(context);
		defineProperty_ptr(context, "x", &x);
		defineProperty_ptr(context, "y", &y);
	}

	int x, y;
};

class GlobalObject : public CL_JSObject
{
//!Construction:
public:
	GlobalObject(CL_JSContext &context)
	: obj(context), obj2(new MemberObject(context)), printerName("LAS-HP"), debugLevel(1)
	{
		newObject(context);
		initStandardClasses(context);

		defineFunction(context, "print", &GlobalObject::print, 1);
		defineFunction_1(context, "test", &GlobalObject::test);
		defineFunction_2(context, "sum", &GlobalObject::sum);
		defineFunction_3(context, "sum3", &GlobalObject::sum3);
		defineFunction_4(context, "sum4", &GlobalObject::sum4);
		defineFunction_5(context, "sum5", &GlobalObject::sum5);
		defineFunction_v1(context, "voidFunc", &GlobalObject::voidFunc);

		defineProperty(context, "debugLevel", &GlobalObject::getDebugLevel, &GlobalObject::setDebugLevel);
		defineProperty_1(context, "value", &GlobalObject::getValue, &GlobalObject::setValue);
		defineProperty_ptr(context, "printerName", &printerName);

		defineProperty_obj(context, "memberObj", &obj);
		defineProperty_objPtr(context, "memberObj2", &obj2);
	}

	~GlobalObject()
	{
//		delete obj2;
	}

//!Implementation:
private:
	MemberObject obj;
	MemberObject *obj2;
	std::string printerName;

	std::string getValue()
	{
		return value;
	}

	void setValue(std::string v)
	{
		value = v;
	}

	int test(std::string str)
	{
		std::cout << "test was called with " << str.c_str() << std::endl;
		return 42;
	}

	float sum(float a, float b)
	{
		return a+b;
	}

	float sum3(float a, float b, float)
	{
		return a+b;
	}

	float sum4(float a, float b, float, float)
	{
		return a+b;
	}

	std::string sum5(float a, float b, float, float, float)
	{
		return "bla bla!";
	}

	void voidFunc(int a)
	{
	}

	CL_JSValue print(CL_JSContext *cx, CL_JSArguments &args)
	{
		if (debugLevel > 0)
		{
			std::string msg = args[0];
			printf("Script Debug: %s\r\n", msg.c_str());
		}

		return CL_JSValue(cx);
	}

	void setDebugLevel(const CL_JSValue &value)
	{
		debugLevel = value;
	}

	CL_JSValue getDebugLevel(CL_JSContext *context)
	{
		return CL_JSValue(context, debugLevel);
	}

	int debugLevel;

	std::string value;
};

class ActiveXObject : public CL_JSObject
{
//!Construction:
public:
	ActiveXObject(CL_JSContext *context, CL_JSArguments &args)
	{
		clsId = args[0];

		newObject(*context);
		defineFunction(*context, "calc", &ActiveXObject::calc, 1);
	}

//!Implementation:
private:
	CL_JSValue calc(CL_JSContext *cx, CL_JSArguments &args)
	{
		char str[1024];
		sprintf(str, "%s.calc called with %d", clsId.c_str(), (int) args[0]);

		return CL_JSValue(cx, std::string(str));
	}

	std::string clsId;
};

int main(int argc, char **argv)
{
	CL_JSRuntime runtime(1024*1024);
	CL_JSContext context(runtime, 8*1024);
	GlobalObject globalObj(context);
	CL_JSClassFactory<ActiveXObject> staticObj("ActiveXObject", 1, context, globalObj);

	char *evalStr = "var obj = new ActiveXObject(\"odbc.connection\"); print(obj.calc(test(\"fisk\"))); debugLevel = 0; print(\"Bla bla\"); printerName;";
	CL_JSValue result = context.evaluateScript(globalObj, evalStr);
	std::cout << "Result: " << result.getString().c_str() << std::endl;

	evalStr = "memberObj2.x;";
	int i = context.evaluateScript(globalObj, evalStr);
	std::cout << "Result: " << i << std::endl;

	return 0;
}