File: runtime_script_value.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (445 lines) | stat: -rw-r--r-- 12,999 bytes parent folder | download | duplicates (2)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//=============================================================================
//
// Runtime script value struct
//
//=============================================================================

#ifndef AGS_ENGINE_SCRIPT_RUNTIME_SCRIPT_VALUE_H
#define AGS_ENGINE_SCRIPT_RUNTIME_SCRIPT_VALUE_H

#include "ags/engine/ac/dynobj/cc_script_object.h"
#include "ags/engine/ac/dynobj/cc_static_array.h"
#include "ags/engine/script/script_api.h"
#include "ags/shared/util/memory.h"

#include "ags/plugins/plugin_base.h"

namespace AGS3 {

enum ScriptValueType {
	kScValUndefined,    // to detect errors
	kScValInteger,      // as strictly 32-bit integer (for integer math)
	kScValFloat,        // as float (for floating point math), 32-bit
	kScValPluginArg,    // an 32-bit value, passed to a script function when called
						// directly by plugin; is allowed to represent object pointer
	kScValStackPtr,     // as a pointer to stack entry
	kScValData,         // as a container for randomly sized data (usually array)
	kScValGlobalVar,    // as a pointer to script variable; used only for global vars,
						// as pointer to local vars must have StackPtr type so that the
						// stack allocation could work
	kScValStringLiteral,// as a pointer to literal string (array of chars)
	kScValStaticArray,  // as a pointer to static global array (of static or dynamic objects)
	kScValScriptObject, // as a pointer to managed script object
	kScValPluginObject, // as a pointer to object managed by plugin (similar to
						// kScValScriptObject, but has backward-compatible limitations)
	kScValStaticFunction,// as a pointer to static function
	kScValPluginFunction,// temporary workaround for plugins (unsafe function ptr)
	kScValObjectFunction,// as a pointer to object member function, gets object pointer as
						 // first parameter
	kScValCodePtr       // as a pointer to element in byte-code array
};

struct RuntimeScriptValue {
public:
	RuntimeScriptValue() {
		Type = kScValUndefined;
		IValue = 0;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 0;
	}

	RuntimeScriptValue(int32_t val) {
		Type = kScValInteger;
		IValue = val;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 4;
	}

	ScriptValueType Type;
	Common::String methodName;
	// The 32-bit value used for integer/float math and for storing
	// variable/element offset relative to object (and array) address
	union {
		int32_t     IValue; // access Value as int32 type
		float       FValue; // access Value as float type
	};
	// Pointer is used for storing... pointers - to objects, arrays,
	// functions and stack entries (other RSV)
	union {
		void	*Ptr;	// generic data pointer
		uint8_t *PtrU8;	// byte buffer pointer
		char	*CStr;	// char buffer pointer
		RuntimeScriptValue *RValue;// access ptr as a pointer to Runtime Value
		ScriptAPIFunction *SPfn;  // access ptr as a pointer to Script API Static Function
		ScriptAPIObjectFunction *ObjPfn; // access ptr as a pointer to Script API Object Function
	};
	// TODO: separation to Ptr and MgrPtr is only needed so far as there's
	// a separation between Script*, Dynamic* and game entity classes.
	// Once those classes are merged, it will no longer be needed.
	union {
		void			 *MgrPtr;  // generic object manager pointer
		IScriptObject	 *ObjMgr;  // script object manager
		CCStaticArray	 *ArrMgr;  // static array manager
	};
	// The "real" size of data, either one stored in I/FValue,
	// or the one referenced by Ptr. Used for calculating stack
	// offsets.
	// Original AGS scripts always assumed pointer is 32-bit.
	// Therefore for stored pointers Size is always 4 both for x32
	// and x64 builds, so that the script is interpreted correctly.
	int             Size;

	inline bool IsValid() const {
		return Type != kScValUndefined;
	}

	inline bool IsNull() const {
		return Ptr == nullptr && IValue == 0;
	}

	inline bool GetAsBool() const {
		return !IsNull();
	}

	inline void *GetPtrWithOffset() const {
		return PtrU8 + IValue;
	}

	inline void *GetRValuePtrWithOffset() const {
		return static_cast<uint8_t *>(RValue->GetPtrWithOffset()) + this->IValue;
	}

	inline RuntimeScriptValue &Invalidate() {
		*this = RuntimeScriptValue();
		return *this;
	}

	inline RuntimeScriptValue &SetUInt8(uint8_t val) {
		Type = kScValInteger;
		methodName.clear();
		IValue = val;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 1;
		return *this;
	}

	inline RuntimeScriptValue &SetInt16(int16_t val) {
		Type = kScValInteger;
		methodName.clear();
		IValue = val;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 2;
		return *this;
	}

	inline RuntimeScriptValue &SetInt32(int32_t val) {
		Type = kScValInteger;
		methodName.clear();
		IValue = val;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetFloat(float val) {
		Type = kScValFloat;
		methodName.clear();
		FValue = val;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetInt32AsBool(bool val) {
		return SetInt32(val ? 1 : 0);
	}

	inline RuntimeScriptValue &SetFloatAsBool(bool val) {
		return SetFloat(val ? 1.0F : 0.0F);
	}

	inline RuntimeScriptValue &SetPluginArgument(int32_t val) {
		Type = kScValPluginArg;
		methodName.clear();
		IValue = val;
		Ptr = nullptr;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetStackPtr(RuntimeScriptValue *stack_entry) {
		Type = kScValStackPtr;
		methodName.clear();
		IValue = 0;
		RValue = stack_entry;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetData(void *data, int size) {
		Type = kScValData;
		methodName.clear();
		IValue = 0;
		Ptr = data;
		MgrPtr = nullptr;
		Size = size;
		return *this;
	}

	inline RuntimeScriptValue &SetGlobalVar(RuntimeScriptValue *glvar_value) {
		Type = kScValGlobalVar;
		methodName.clear();
		IValue = 0;
		RValue = glvar_value;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	// TODO: size?
	inline RuntimeScriptValue &SetStringLiteral(const char *str) {
		Type = kScValStringLiteral;
		methodName.clear();
		IValue = 0;
		Ptr = const_cast<char *>(str);
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetStaticArray(void *object, CCStaticArray *manager) {
		Type = kScValStaticArray;
		methodName.clear();
		IValue = 0;
		Ptr = object;
		ArrMgr = manager;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetScriptObject(void *object, IScriptObject *manager) {
		Type = kScValScriptObject;
		methodName.clear();
		IValue = 0;
		Ptr = object;
		ObjMgr = manager;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetPluginObject(void *object, IScriptObject *manager) {
		Type = kScValPluginObject;
		methodName.clear();
		IValue = 0;
		Ptr = object;
		ObjMgr = manager;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetScriptObject(ScriptValueType type, void *object, IScriptObject *manager) {
		Type = type;
		IValue = 0;
		Ptr = object;
		ObjMgr = manager;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetStaticFunction(ScriptAPIFunction *pfn) {
		Type = kScValStaticFunction;
		methodName.clear();
		IValue = 0;
		SPfn = pfn;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetPluginMethod(Plugins::ScriptContainer *sc, const Common::String &method) {
		Type = kScValPluginFunction;
		methodName = method;
		Ptr = sc;
		MgrPtr = nullptr;
		IValue = 0;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetObjectFunction(ScriptAPIObjectFunction *pfn) {
		Type = kScValObjectFunction;
		methodName.clear();
		IValue = 0;
		ObjPfn = pfn;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue &SetCodePtr(void *ptr) {
		Type = kScValCodePtr;
		methodName.clear();
		IValue = 0;
		Ptr = ptr;
		MgrPtr = nullptr;
		Size = 4;
		return *this;
	}

	inline RuntimeScriptValue operator !() const {
		return RuntimeScriptValue().SetInt32AsBool(!GetAsBool());
	}

	inline bool operator ==(const RuntimeScriptValue &rval) const {
		if (rval.Type == kScValPluginFunction) {
			assert(!rval.methodName.empty());
			return (Type == kScValPluginFunction) && (rval.methodName == methodName);
		}

		return ((intptr_t)Ptr + (intptr_t)IValue) == ((intptr_t)rval.Ptr + (intptr_t)rval.IValue);
	}
	inline bool operator !=(const RuntimeScriptValue &rval) const {
		return !(*this == rval);
	}

	// FIXME: find out all certain cases when we are reading a pointer and store it
	// as 32-bit value here. There should be a solution to distinct these cases and
	// store value differently, otherwise it won't work for 64-bit build.
	inline RuntimeScriptValue ReadValue() const {
		switch (this->Type) {
		case kScValStackPtr: {
			// FIXME: join the kScValStackPtr with kScValData using some flag?
			switch (RValue->Type) {
			case kScValData:
				// read from the stack memory buffer
				return RuntimeScriptValue().SetInt32(*(int32_t *)(GetRValuePtrWithOffset()));
			default:
				// return the stack entry itself
				return *RValue;
			}
		}
		case kScValGlobalVar: {
			// FIXME: join the kScValGlobalVar with kScValData using some flag?
			switch (RValue->Type) {
			case kScValData:
				// read from the global memory buffer
				return RuntimeScriptValue().SetInt32(AGS::Shared::Memory::ReadInt32LE(GetRValuePtrWithOffset()));
			default:
				// return the gvar entry itself
				return *RValue;
			}
		}
		case kScValStaticArray:
		case kScValScriptObject:
			return RuntimeScriptValue().SetInt32(this->ObjMgr->ReadInt32(this->Ptr, this->IValue));
		default:
			return RuntimeScriptValue().SetInt32(*(int32_t *)this->GetPtrWithOffset());
		}
	}

	// Notice, that there are only two valid cases when a pointer may be written:
	// when the destination is a stack entry or global variable of free type
	// (not kScValData type).
	// In any other case, only the numeric value (integer/float) will be written.
	inline void WriteValue(const RuntimeScriptValue &rval) {
		switch (this->Type) {
		case kScValStackPtr: {
			// FIXME: join the kScValStackPtr with kScValData using some flag?
			switch (RValue->Type) {
			case kScValData:
				// write into the stack memory buffer
				*(int32_t *)(GetRValuePtrWithOffset()) = rval.IValue;
				break;
			default:
				// write into the stack entry
				*RValue = rval;
				// On stack we assume each item has at least 4 bytes (with exception
				// of arrays - kScValData). This is why we fixup the size in case
				// the assigned value is less (char, int16).
				RValue->Size = 4;
				break;
			}
			break;
		}
		case kScValGlobalVar: {
			// FIXME: join the kScValGlobalVar with kScValData using some flag?
			switch (RValue->Type) {
			case kScValData:
				// write into the global memory buffer
				AGS::Shared::Memory::WriteInt32LE(GetRValuePtrWithOffset(), rval.IValue);
				break;
			default:
				// write into the gvar entry
				*RValue = rval;
				break;
			}
			break;
		}
		case kScValStaticArray:
		case kScValScriptObject: {
			this->ObjMgr->WriteInt32(this->Ptr, this->IValue, rval.IValue);
			break;
		}
		default: {
			*((int32_t *)this->GetPtrWithOffset()) = rval.IValue;
			break;
		}
		}
	}

	Plugins::PluginMethod pluginMethod() const {
		return Plugins::PluginMethod((Plugins::PluginBase *)Ptr, methodName);
	}

	// Helper functions for reading or writing values from/to
	// object, referenced by this Runtime Value.
	// Copy implementation depends on value type.
	uint8_t     ReadByte() const;
	int16_t     ReadInt16() const;
	int32_t     ReadInt32() const;
	void        WriteByte(uint8_t val);
	void        WriteInt16(int16_t val);
	void        WriteInt32(int32_t val);

	// Convert to most simple pointer type by resolving RValue ptrs and applying offsets;
	// non pointer types are left unmodified
	RuntimeScriptValue &DirectPtr();
	// Similar to above, a slightly speed-optimised version for situations when we can
	// tell for certain that we are expecting a pointer to the object and not its (first) field.
	RuntimeScriptValue &DirectPtrObj();
	// Resolve and return direct pointer to the referenced data; non pointer types return IValue
	void *GetDirectPtr() const;
};

} // namespace AGS3

#endif