Debian Codegraph @

terps/tads/tads3/vmtype.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/VMTYPE.H,v 1.3 1999/05/17 02:52:29 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmtype.h - VM types
12 Function
13
14 Notes
15
16 Modified
17 10/21/98 MJRoberts - Creation
18 */
19
20 #ifndef VMTYPE_H
21 #define VMTYPE_H
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "t3std.h"
27 #include "vmerr.h"
28 #include "vmerrnum.h"
29 #include "vmglob.h"
30
31
32 /*
33 * Constant pool/code offset. This is an address of an object in the
34 * pool. Pool offsets are 32-bit values.
35 */
36 typedef uint32_t pool_ofs_t;
37
38 /*
39 * Savepoint ID's are stored in a single byte; since we store many
40 * copies of savepoint ID's (since they need to be stored with each undo
41 * list head), we want to save some space on this type. This limits us
42 * to 256 simultaneous savepoints, but this should be more than we
43 * actually want to keep around anyway, because of the amount of memory
44 * it would consume to try to keep more than that around.
45 */
46 typedef uchar vm_savept_t;
47 const vm_savept_t VM_SAVEPT_MAX = 255;
48
49 /*
50 * Object ID type. VM_INVALID_OBJ is a distinguished value that serves
51 * as an invalid object ID (a null pointer, effectively); no object can
52 * ever have this ID.
53 */
54 typedef uint32_t vm_obj_id_t;
55 const vm_obj_id_t VM_INVALID_OBJ = 0;
56
57 /*
58 * Property ID. Property ID's are 16-bit values. VM_INVALID_PROP is a
59 * distinguished value that serves as an invalid property ID, which can
60 * be used to indicate the absence of a property value.
61 */
62 typedef uint16_t vm_prop_id_t;
63 const vm_prop_id_t VM_INVALID_PROP = 0;
64
65 /*
66 * Maximum recursion depth for recursive equality tests and hash
67 * calculations.
68 *
69 * When we're comparing or hashing a tree of references by value, such as
70 * when we're comparing two vectors or hashing a vector, we'll keep track
71 * of the recursion depth of our tree traversal. If we reach this depth,
72 * we'll throw an error on the assumption that the tree contains cycles and
73 * thus cannot be hashed or compared by value. This depth is chosen to be
74 * large enough that it's unlikely we'll exceed it with acyclical trees,
75 * but small enough that we probably won't blow the C++ stack before we
76 * reach this depth.
77 */
78 const int VM_MAX_TREE_DEPTH_EQ = 256;
79
80 /*
81 * Datatypes
82 */
83 enum vm_datatype_t
84 {
85 /* nil - doubles as a null pointer and a boolean false */
86 VM_NIL = 1,
87
88 /* true - boolean true */
89 VM_TRUE,
90
91 /*
92 * Stack pointer (this is used to store a pointer to the enclosing
93 * frame in a stack frame). This is a native machine pointer.
94 */
95 VM_STACK,
96
97 /*
98 * Code pointer (this is used to store a pointer to the return
99 * address in a stack frame, for example). This is a native machine
100 * pointer. This differs from VM_CODEOFS in that this is a native
101 * machine pointer.
102 */
103 VM_CODEPTR,
104
105 /* object reference */
106 VM_OBJ,
107
108 /* property ID */
109 VM_PROP,
110
111 /* 32-bit signed integer */
112 VM_INT,
113
114 /*
115 * string constant value - the value is an offset into the constant
116 * pool of the string descriptor
117 */
118 VM_SSTRING,
119
120 /*
121 * self-printing string value - the value is an offset into the
122 * constant pool of the string descriptor
123 */
124 VM_DSTRING,
125
126 /*
127 * list constant - the value is an offset into the constant pool of
128 * the list descriptor
129 */
130 VM_LIST,
131
132 /*
133 * byte-code constant offset - this is an offset into the byte-code
134 * pool. This differs from VM_CODEPTR in that this is an offset in
135 * the byte-code constant pool rather than a native machine pointer.
136 *
137 */
138 VM_CODEOFS,
139
140 /*
141 * function pointer - this is represented as an offset into the
142 * byte-code pool. This differs from VM_CODEOFS in that the code
143 * referenced by a VM_CODEOFS value is generally invoked directly
144 * whenever the value is evaluated, whereas VM_FUNCPTR values are
145 * used to convey function pointers, so the underlying code is not
146 * executed implicitly on evaluation of such a value but must be
147 * explicitly invoked.
148 */
149 VM_FUNCPTR,
150
151 /*
152 * This is a special pseudo-type used to indicate that a value is
153 * not present. This differs from nil, in that nil is a null
154 * reference or false value, whereas this indicates that there's no
155 * specified value at all. This is used, for example, to indicate
156 * in an undo record that a property did not previously exist.
157 */
158 VM_EMPTY,
159
160 /*
161 * This is a special pseudo-type used to indicate that evaluating an
162 * expression requires executing system code. The value stored is a
163 * pointer to a constant CVmNativeCodeDesc object, which describes a
164 * native code method.
165 */
166 VM_NATIVE_CODE,
167
168 /*
169 * Enumerated constant
170 */
171 VM_ENUM,
172
173 /*
174 * Built-in function pointer. This is represented as an integer value
175 * with the BIF table entry index (the function set ID) in the high 16
176 * bits, and the function index (the index of the function within its
177 * function set) in the low 16 bits.
178 */
179 VM_BIFPTR,
180
181 /*
182 * Special internal pseudo-type for execute-on-evaluation objects.
183 * This is used when an anonymous object or dynamically created string
184 * is stored in an object property *as a method*, meaning that the
185 * object is executed upon evaluation rather than simply returned as an
186 * object value. This is never stored in an image file, but it can be
187 * stored in a saved game file. It's never used in a value that's
188 * visible to user code; it's only for internal storage within
189 * TadsObject property tables and the like.
190 */
191 VM_OBJX,
192
193 /* the equivalent of VM_OBJX for built-in function pointers */
194 VM_BIFPTRX,
195
196 /*
197 * First invalid type ID. Tools (such as compilers and debuggers)
198 * can use this ID and any higher ID values to flag their own
199 * internal types.
200 */
201 VM_FIRST_INVALID_TYPE
202 };
203
204 /*
205 * Macro to create a private type constant for internal use in a tool. The
206 * compiler uses this to create special non-VM values for its own use
207 * during compilation. Types created with this macro must never appear in
208 * an image file, and must never be exposed to user bytecode via any APIs.
209 */
210 #define VM_MAKE_INTERNAL_TYPE(idx) \
211 ((vm_datatype_t)(((int)VM_FIRST_INVALID_TYPE) + (idx)))
212
213 /*
214 * Value container. Local variables, stack locations, and other value
215 * holders use this structure to store a value and its type.
216 */
217 struct vm_val_t
218 {
219 vm_datatype_t typ;
220 union
221 {
222 /* stack/code pointer */
223 const void *ptr;
224
225 /* object reference */
226 vm_obj_id_t obj;
227
228 /* property ID */
229 vm_prop_id_t prop;
230
231 /* 32-bit integer */
232 int32_t intval;
233
234 /* enumerated constant */
235 uint32_t enumval;
236
237 /* sstring/dstring/list constant pool offset/pcode pool offset */
238 pool_ofs_t ofs;
239
240 /* native code descriptor */
241 const class CVmNativeCodeDesc *native_desc;
242
243 /* pointer to built-in function */
244 struct
245 {
246 unsigned short set_idx;
247 unsigned short func_idx;
248 } bifptr;
249 } val;
250
251 /* set various types of values */
252 void set_empty() { typ = VM_EMPTY; }
253 void set_nil() { typ = VM_NIL; }
254 void set_true() { typ = VM_TRUE; }
255 void set_stack(void *ptr) { typ = VM_STACK; val.ptr = ptr; }
256 void set_codeptr(const void *ptr) { typ = VM_CODEPTR; val.ptr = ptr; }
257 void set_obj(vm_obj_id_t obj) { typ = VM_OBJ; val.obj = obj; }
258 void set_propid(vm_prop_id_t prop) { typ = VM_PROP; val.prop = prop; }
259 void set_int(int32_t intval) { typ = VM_INT; val.intval = intval; }
260 void set_enum(uint32_t enumval) { typ = VM_ENUM; val.enumval = enumval; }
261 void set_sstring(pool_ofs_t ofs) { typ = VM_SSTRING; val.ofs = ofs; }
262 void set_dstring(pool_ofs_t ofs) { typ = VM_DSTRING; val.ofs = ofs; }
263 void set_list(pool_ofs_t ofs) { typ = VM_LIST; val.ofs = ofs; }
264 void set_codeofs(pool_ofs_t ofs) { typ = VM_CODEOFS; val.ofs = ofs; }
265 void set_fnptr(pool_ofs_t ofs) { typ = VM_FUNCPTR; val.ofs = ofs; }
266 void set_bifptr(ushort set_idx, ushort func_idx)
267 {
268 typ = VM_BIFPTR;
269 val.bifptr.set_idx = set_idx;
270 val.bifptr.func_idx = func_idx;
271 }
272 void set_native(const class CVmNativeCodeDesc *desc)
273 { typ = VM_NATIVE_CODE; val.native_desc = desc; }
274
275 /*
276 * Set nil, specifically for a value that might be interpreted as an
277 * object ID. This sets the object ID field to invalid in addition to
278 * setting the value type to nil, so that callers that just look at the
279 * object ID will see an explicitly invalid object value rather than
280 * something random.
281 */
282 void set_nil_obj() { typ = VM_NIL; val.obj = VM_INVALID_OBJ; }
283
284 /*
285 * set an object or nil value: if the object ID is VM_INVALID_OBJ,
286 * we'll set the type to nil
287 */
288 void set_obj_or_nil(vm_obj_id_t obj)
289 {
290 /* set the object value initially */
291 typ = VM_OBJ;
292 val.obj = obj;
293
294 /* if the object is invalid, set the type to nil */
295 if (obj == VM_INVALID_OBJ)
296 typ = VM_NIL;
297 }
298
299 /* get an object, or a null pointer if it's not an object */
300 vm_obj_id_t get_as_obj() const { return typ == VM_OBJ ? val.obj : 0; }
301
302 /* is this an object of the given class? */
303 int is_instance_of(VMG_ vm_obj_id_t cls) const;
304
305 /* set to an integer giving the datatype of the given value */
306 void set_datatype(VMG_ const vm_val_t *val);
307
308 /* set to nil if 'val' is zero, true if 'val' is non-zero */
309 void set_logical(int v) { typ = (v != 0 ? VM_TRUE : VM_NIL); }
310
311 /* determine if the value is logical (nil or true) */
312 int is_logical() const { return (typ == VM_NIL || typ == VM_TRUE); }
313
314 /*
315 * Get a logical as numeric TRUE or FALSE. This doesn't perform
316 * any type checking; the caller must ensure that the value is
317 * either true or nil, or this may return meaningless results.
318 */
319 int get_logical() const { return (typ == VM_TRUE); }
320
321 /* get as logical, checking type */
322 int get_logical_only() const
323 {
324 if (typ == VM_TRUE)
325 return TRUE;
326 else if (typ == VM_NIL)
327 return FALSE;
328 else
329 {
330 err_throw(VMERR_BAD_TYPE_BIF);
331 AFTER_ERR_THROW(return FALSE;)
332 }
333 }
334
335 /*
336 * Get the underlying string constant value. If the value does not
337 * have an underlying string constant (because it is of a type that
338 * does not store a string value), this will return null.
339 */
340 const char *get_as_string(VMG0_) const;
341
342 /*
343 * Cast the value to a string. This attempts to reinterpret the value
344 * as a string.
345 */
346 const char *cast_to_string(VMG_ vm_val_t *new_str) const;
347
348 /*
349 * Get the underlying list constant value. If the value does not
350 * have an underlying list constant (because it is of a type that
351 * does not store list data), this returns null.
352 */
353 const char *get_as_list(VMG0_) const;
354
355 /*
356 * Is the value indexable as a list? This returns true for types that
357 * have contiguous integer indexes from 1..number of elements.
358 */
359 int is_listlike(VMG0_) const;
360
361 /*
362 * For a list-like object, get the number of elements. If this isn't a
363 * list-like object, returns -1.
364 */
365 int ll_length(VMG0_) const;
366
367 /*
368 * For a list-like object, get an indexed element. The index is
369 * 1-based, per our usual conventions.
370 */
371 void ll_index(VMG_ vm_val_t *ret, const vm_val_t *idx) const;
372
373 /* index with a 1-based integer */
374 void ll_index(VMG_ vm_val_t *ret, int idx) const
375 {
376 vm_val_t vidx;
377 vidx.set_int(idx);
378 ll_index(vmg_ ret, &vidx);
379 }
380
381 /* get as a native code pointer value */
382 const uchar *get_as_codeptr(VMG0_) const;
383
384 /*
385 * Is this a function pointer of some kind? This returns true if it's
386 * a plain function pointer, a built-in function pointer, or an object
387 * with an invoker (which covers anonymous and dynamic functions).
388 */
389 int is_func_ptr(VMG0_) const;
390
391 /*
392 * Convert a numeric value to an integer value. If the value isn't
393 * numeric, throws an error.
394 */
395 void num_to_logical()
396 {
397 /* check the type */
398 if (typ == VM_INT)
399 {
400 /* it's an integer - treat 0 as nil, all else as true */
401 typ = (val.intval == 0 ? VM_NIL : VM_TRUE);
402 }
403 else
404 {
405 /* it's not a number - throw an error */
406 err_throw(VMERR_NO_LOG_CONV);
407 }
408 }
409
410 /* determine if the value is an integer */
411 int is_int() const { return (typ == VM_INT); }
412
413 /* get the value as an integer, throwing an error if it's any other type */
414 int32_t get_as_int() const
415 {
416 /* check the type */
417 if (typ == VM_INT)
418 {
419 /* it's an integer already - return the value directly */
420 return val.intval;
421 }
422 else
423 {
424 /* not an integer - throw an error */
425 err_throw(VMERR_NUM_VAL_REQD);
426 AFTER_ERR_THROW(return 0;)
427 }
428 }
429
430 /*
431 * determine if the type is numeric - this returns true for integer and
432 * BigNumber values, and could match future types that are numeric
433 */
434 int is_numeric(VMG0_) const
435 { return typ == VM_INT || nonint_is_numeric(vmg0_); }
436
437 /*
438 * Promote an integer to this numeric type. For example, if 'this' is
439 * a BigNumber, this creates a BigNumber representation of val. This
440 * converts the value in place, replacing '*val' with the promoted
441 * value.
442 */
443 void promote_int(VMG_ vm_val_t *val) const;
444
445 /*
446 * Convert a numeric value to an integer. This converts any type for
447 * which is_numeric() returns true, but doesn't convert non-numeric
448 * types.
449 */
450 int32_t num_to_int(VMG0_) const
451 { return typ == VM_INT ? val.intval : nonint_num_to_int(vmg0_); }
452
453 /*
454 * Convert a numeric value to a double. This converts any type for
455 * which is_numeric() returns true, but doesn't convert non-numeric
456 * types.
457 */
458 double num_to_double(VMG0_) const
459 {
460 return typ == VM_INT
461 ? (double)val.intval
462 : nonint_num_to_double(vmg0_);
463 }
464
465 /*
466 * Cast to an integer. This is more aggressive than num_to_int(), in
467 * that we actively try to convert non-numeric types.
468 */
469 int32_t cast_to_int(VMG0_) const
470 { return typ == VM_INT ? val.intval : nonint_cast_to_int(vmg0_); }
471
472 /*
473 * Cast to a numeric type - integer or BigNumber. If the value is
474 * already numeric, we'll return the value unchanged. Otherwise, if an
475 * automatic conversion to a numeric type is possible, we'll return the
476 * converted value. We'll return the "simplest" type that can
477 * precisely represent the value, meaning the type that uses the least
478 * memory and that's fastest for computations.
479 */
480 void cast_to_num(VMG_ vm_val_t *val) const;
481
482 /*
483 * determine if the numeric value is zero; throws an error if the
484 * value is not numeric
485 */
486 int num_is_zero() const
487 {
488 /* check the type */
489 if (typ == VM_INT)
490 {
491 /* check the integer value to see if it's zero */
492 return (val.intval == 0);
493 }
494 else
495 {
496 /* it's not a number */
497 err_throw(VMERR_NUM_VAL_REQD);
498
499 /* in case the compiler doesn't know we'll never get here */
500 AFTER_ERR_THROW(return 0;)
501 }
502 }
503
504 /*
505 * Determine if this value equals a given value. The nature of the
506 * match depends on the type of this value:
507 *
508 * integers, property ID's, code offsets: the types and values must
509 * match exactly.
510 *
511 * string and list constants: the other value must either be the same
512 * type of constant, or an object that has an underlying value of the
513 * same type; and the contents of the strings or lists must match.
514 *
515 * objects: the match depends on the type of the object. We invoke the
516 * object's virtual equals() routine to make this determination.
517 *
518 * 'depth' has the same meaning as in calc_hash().
519 */
520 int equals(VMG_ const vm_val_t *val, int depth = 0) const;
521
522 /*
523 * Calculate a hash for the value. The meaning of the hash varies by
524 * type, but is stable for a given value. 'depth' is a recursion depth
525 * counter, with the same meaning as in CVmObject::calc_hash().
526 */
527 uint calc_hash(VMG_ int depth = 0) const;
528
529 /*
530 * Compare this value to the given value. Returns a value greater than
531 * zero if this value is greater than 'val', a value less than zero if
532 * this value is less than 'val', or 0 if the two values are equal.
533 * Throws an error if the two values are not comparable.
534 *
535 * By far the most common type of comparison is between integers, so we
536 * test in-line to see if we have two integer values, and if so, use a
537 * fast in-line comparison. If we don't have two integers, we'll use
538 * our full out-of-line test, which will look at other more interesting
539 * type combinations.
540 */
541 int compare_to(VMG_ const vm_val_t *b) const
542 {
543 if (typ == VM_INT && b->typ == VM_INT)
544 return (val.intval > b->val.intval
545 ? 1 : val.intval < b->val.intval ? -1 : 0);
546 else
547 return gen_compare_to(vmg_ b);
548 }
549
550 /*
551 * relative value comparisons
552 */
553
554 /* self > b */
555 int is_gt(VMG_ const vm_val_t *b) const
556 {
557 if (typ == VM_INT && b->typ == VM_INT)
558 return val.intval > b->val.intval;
559 else
560 return gen_compare_to(vmg_ b) > 0;
561 }
562
563 /* self >= b */
564 int is_ge(VMG_ const vm_val_t *b) const
565 {
566 if (typ == VM_INT && b->typ == VM_INT)
567 return val.intval >= b->val.intval;
568 else
569 return gen_compare_to(vmg_ b) >= 0;
570 }
571
572 /* self < b */
573 int is_lt(VMG_ const vm_val_t *b) const
574 {
575 if (typ == VM_INT && b->typ == VM_INT)
576 return val.intval < b->val.intval;
577 else
578 return gen_compare_to(vmg_ b) < 0;
579 }
580
581 /* self <= b */
582 int is_le(VMG_ const vm_val_t *b) const
583 {
584 if (typ == VM_INT && b->typ == VM_INT)
585 return val.intval <= b->val.intval;
586 else
587 return gen_compare_to(vmg_ b) <= 0;
588 }
589
590 private:
591 /* out-of-line comparison, used when we don't have two integers */
592 int gen_compare_to(VMG_ const vm_val_t *val) const;
593
594 /*
595 * internal int conversion and casting - we separate these from
596 * num_to_int and cast_to_int so that cast_to_int can be inlined for
597 * the common case where the value is already an int
598 */
599 int nonint_is_numeric(VMG0_) const;
600 int32_t nonint_num_to_int(VMG0_) const;
601 double nonint_num_to_double(VMG0_) const;
602 int32_t nonint_cast_to_int(VMG0_) const;
603 };
604
605 /* ------------------------------------------------------------------------ */
606 /*
607 * Native code descriptor. This describes a native method call of an
608 * intrinsic class object.
609 */
610 class CVmNativeCodeDesc
611 {
612 public:
613 /* create a descriptor with an exact number of arguments */
614 CVmNativeCodeDesc(int argc)
615 {
616 /* remember the parameters - there are no optional arguments */
617 min_argc_ = argc;
618 opt_argc_ = 0;
619 varargs_ = FALSE;
620 }
621
622 /* create a descriptor with optional arguments (but not varargs) */
623 CVmNativeCodeDesc(int min_argc, int opt_argc)
624 {
625 /* remember the parameters */
626 min_argc_ = min_argc;
627 opt_argc_ = opt_argc;
628 varargs_ = FALSE;
629 }
630
631 /* create a descriptor with optional arguments and/or varargs */
632 CVmNativeCodeDesc(int min_argc, int opt_argc, int varargs)
633 {
634 /* remember the parameters */
635 min_argc_ = min_argc;
636 opt_argc_ = opt_argc;
637 varargs_ = varargs;
638 }
639
640 /* check the given number of arguments for validity */
641 int args_ok(int argc) const
642 {
643 /*
644 * the actual parameters must number at least the minimum, and
645 * cannot exceed the maximum (i.e., the minimum plus the
646 * optionals) unless we have varargs, in which case there is no
647 * maximum
648 */
649 return (argc >= min_argc_
650 && (varargs_ || argc <= min_argc_ + opt_argc_));
651 }
652
653 /* minimum argument count */
654 int min_argc_;
655
656 /* number of optional named arguments beyond the minimum */
657 int opt_argc_;
658
659 /*
660 * true -> varargs: any number of arguments greater than or equal to
661 * min_argc_ are valid
662 */
663 int varargs_;
664 };
665
666 /* ------------------------------------------------------------------------ */
667 /*
668 * String handling - these routines are provided as covers to allow for
669 * easier adjustment for Unicode or other encodings. Don't include these
670 * if we're compiling interface routines for the HTML TADS environment,
671 * since HTML TADS has its own similar definitions for these, and we don't
672 * need these for interface code.
673 *
674 * NOTE: The purpose of this macro is to indicate that we're being
675 * #included by an HTML TADS source file - that is, the current compile run
676 * is for an htmltads/xxx.cpp file. Do NOT #define this macro when
677 * compiling tads3/xxx.cpp files. The tads3/xxx.cpp files have absolutely
678 * no explicit dependencies on the htmltads subsystem and thus don't
679 * #include the htmltads headers that would provide the conflicting
680 * interface definitions for these functions. The conflict is in the other
681 * direction: some of the htmltads source files explicitly depend on tads3
682 * headers, so they indirectly #include this header. So, this macro needs
683 * to be defined only when compiling htmltads/xxx.cpp files.
684 */
685 #ifdef T3_COMPILING_FOR_HTML
686 #include "tadshtml.h"
687 #else
688
689 inline size_t get_strlen(const textchar_t *str) { return strlen(str); }
690 inline void do_strcpy(textchar_t *dst, const textchar_t *src)
691 { strcpy(dst, src); }
692
693 #endif /* T3_COMPILING_FOR_HTML */
694
695 /* ------------------------------------------------------------------------ */
696 /*
697 * Portable Binary Representations. When we store certain types of
698 * information in memory, we store it in a format that is identical to
699 * the format we use in portable binary files; using this format allows
700 * us to read and write binary files as byte images, without any
701 * interpretation, which greatly improves I/O performance in many cases.
702 */
703
704 /*
705 * Portable binary LENGTH indicator. This is used to store length
706 * prefixes for strings, lists, and similar objects. We use a UINT2
707 * (16-bit unsigned integer) for this type of value.
708 */
709 const size_t VMB_LEN = 2;
710 inline void vmb_put_len(char *buf, size_t len) { oswp2(buf, len); }
711 inline size_t vmb_get_len(const char *buf) { return osrp2(buf); }
712
713 /*
714 * Portable binary unsigned 2-byte integer
715 */
716 const size_t VMB_UINT2 = 2;
717 inline void vmb_put_uint2(char *buf, uint16_t i) { oswp2(buf, i); }
718 inline uint16_t vmb_get_uint2(const char *buf) { return osrp2(buf); }
719
720 /*
721 * Portable binary unsigned 4-byte integer
722 */
723 const size_t VMB_UINT4 = 4;
724 inline void vmb_put_uint4(char *buf, uint32_t i) { oswp4(buf, i); }
725 inline uint32_t vmb_get_uint4(const char *buf) { return osrp4(buf); }
726
727 /*
728 * Portable binary signed 4-byte integer
729 */
730 const size_t VMB_INT4 = 4;
731 inline void vmb_put_int4(char *buf, int32_t i) { oswp4s(buf, i); }
732 inline int32_t vmb_get_int4(const char *buf) { return osrp4s(buf); }
733
734 /*
735 * Portable binary object ID.
736 */
737 const size_t VMB_OBJECT_ID = 4;
738 inline void vmb_put_objid(char *buf, vm_obj_id_t obj) { oswp4(buf, obj); }
739 inline vm_obj_id_t vmb_get_objid(const char *buf) { return t3rp4u(buf); }
740
741 /*
742 * Portable binary property ID
743 */
744 const size_t VMB_PROP_ID = 2;
745 inline void vmb_put_propid(char *buf, vm_obj_id_t prop) { oswp2(buf, prop); }
746 inline vm_prop_id_t vmb_get_propid(const char *buf) { return osrp2(buf); }
747
748 /*
749 * Portable data holder. This is used to store varying-type data items;
750 * for example, this is used to store an element in a list, or the value
751 * of a property in an object. This type of value stores a one-byte
752 * prefix indicating the type of the value, and a four-byte area in
753 * which the value is stored. The actual use of the four-byte value
754 * area depends on the type.
755 */
756 const size_t VMB_DATAHOLDER = 5;
757
758 /* offset from a portable data holder pointer to the data value */
759 const size_t VMB_DH_DATAOFS = 1;
760
761 /* store a portable dataholder from a vm_val_t */
762 void vmb_put_dh(char *buf, const vm_val_t *val);
763
764 /* store a nil value in a portable dataholder */
765 inline void vmb_put_dh_nil(char *buf) { buf[0] = VM_NIL; }
766
767 /* store an object value in a portable dataholder */
768 inline void vmb_put_dh_obj(char *buf, vm_obj_id_t obj)
769 { buf[0] = VM_OBJ; vmb_put_objid(buf + 1, obj); }
770
771 /* store a property value in a portable dataholder */
772 inline void vmb_put_dh_prop(char *buf, vm_prop_id_t prop)
773 { buf[0] = VM_PROP; vmb_put_propid(buf + 1, prop); }
774
775 /* get the value portion of a vm_val_t from a portable dataholder */
776 void vmb_get_dh_val(const char *buf, vm_val_t *val);
777
778 /* get the type from a portable dataholder */
779 inline vm_datatype_t vmb_get_dh_type(const char *buf)
780 { return (vm_datatype_t)buf[0]; }
781
782 /* get a vm_val_t from a portable dataholder */
783 inline void vmb_get_dh(const char *buf, vm_val_t *val)
784 { val->typ = vmb_get_dh_type(buf); vmb_get_dh_val(buf, val); }
785
786 /* get an object value from a portable dataholder */
787 inline vm_obj_id_t vmb_get_dh_obj(const char *buf)
788 { return (vm_obj_id_t)t3rp4u(buf+1); }
789
790 /* get an integer value from a portable dataholder */
791 inline int32_t vmb_get_dh_int(const char *buf)
792 { return (int32_t)osrp4s(buf+1); }
793
794 /* get a property ID value from a portable dataholder */
795 inline vm_prop_id_t vmb_get_dh_prop(const char *buf)
796 { return (vm_prop_id_t)osrp2(buf+1); }
797
798 /* get a constant offset value from a portable dataholder */
799 inline pool_ofs_t vmb_get_dh_ofs(const char *buf)
800 { return (pool_ofs_t)t3rp4u(buf+1); }
801
802
803 #endif /* VMTYPE_H */
804