| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmanonfn.cpp - anonymous function metaclass |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 03/21/00 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include "t3std.h" |
| 24 | #include "vmobj.h" |
| 25 | #include "vmanonfn.h" |
| 26 | #include "vmerr.h" |
| 27 | #include "vmerrnum.h" |
| 28 | #include "vmrun.h" |
| 29 | #include "vmstack.h" |
| 30 | #include "vmpredef.h" |
| 31 | |
| 32 | |
| 33 | /* ------------------------------------------------------------------------ */ |
| 34 | /* |
| 35 | * Statics |
| 36 | */ |
| 37 | /* metaclass registration object */ |
| 38 | static CVmMetaclassAnonFn metaclass_reg_obj; |
| 39 | CVmMetaclass *CVmObjAnonFn::metaclass_reg_ = &metaclass_reg_obj; |
| 40 | |
| 41 | |
| 42 | /* ------------------------------------------------------------------------ */ |
| 43 | /* |
| 44 | * create from stack arguments |
| 45 | */ |
| 46 | vm_obj_id_t CVmObjAnonFn::create_from_stack(VMG_ const uchar **pc_ptr, |
| 47 | uint argc) |
| 48 | { |
| 49 | vm_obj_id_t id; |
| 50 | vm_val_t funcptr; |
| 51 | CVmObjAnonFn *new_obj; |
| 52 | uint idx; |
| 53 | |
| 54 | /* at least one argument is required (the function pointer) */ |
| 55 | if (argc < 1) |
| 56 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 57 | |
| 58 | /* retrieve our function pointer argument */ |
| 59 | G_stk->pop(&funcptr); |
| 60 | if (funcptr.typ == VM_FUNCPTR) |
| 61 | { |
| 62 | /* it's a regular function pointer - accept it */ |
| 63 | } |
| 64 | else if (funcptr.typ == VM_OBJ |
| 65 | && vm_objp(vmg_ funcptr.val.obj)->get_invoker(vmg_ 0)) |
| 66 | { |
| 67 | /* it's a pointer to an invokable object - accept it */ |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | /* it's not a valid function pointer */ |
| 72 | err_throw(VMERR_FUNCPTR_VAL_REQD); |
| 73 | } |
| 74 | |
| 75 | /* create the new object */ |
| 76 | id = vm_new_id(vmg_ FALSE, TRUE, FALSE); |
| 77 | |
| 78 | /* create the new object, giving it one slot per constructor argument */ |
| 79 | new_obj = new (vmg_ id) CVmObjAnonFn(vmg_ argc); |
| 80 | |
| 81 | /* set the first element to our function pointer */ |
| 82 | new_obj->set_element(0, &funcptr); |
| 83 | |
| 84 | /* set the remaining elements to the context objects */ |
| 85 | for (idx = 1 ; idx < argc ; ++idx) |
| 86 | { |
| 87 | vm_val_t val; |
| 88 | |
| 89 | /* pop this value */ |
| 90 | G_stk->pop(&val); |
| 91 | |
| 92 | /* set the element */ |
| 93 | new_obj->set_element(idx, &val); |
| 94 | } |
| 95 | |
| 96 | /* return the new object ID */ |
| 97 | return id; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Invoke |
| 102 | */ |
| 103 | int CVmObjAnonFn::get_invoker(VMG_ vm_val_t *val) |
| 104 | { |
| 105 | /* our first vector element is our function pointer */ |
| 106 | if (val != 0) |
| 107 | { |
| 108 | /* get the function pointer */ |
| 109 | get_element(0, val); |
| 110 | |
| 111 | /* if this is itself an invokable value, get its invoker */ |
| 112 | if (val->typ == VM_OBJ) |
| 113 | return vm_objp(vmg_ val->val.obj)->get_invoker(vmg_ val); |
| 114 | } |
| 115 | |
| 116 | /* we are indeed invokable */ |
| 117 | return TRUE; |
| 118 | } |
| 119 |