cfad47cfa3/tads3/vmanonfn.cpp

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
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 "vmobj.h"
24
#include "vmanonfn.h"
25
#include "vmerr.h"
26
#include "vmerrnum.h"
27
#include "vmrun.h"
28
#include "vmstack.h"
29
#include "vmpredef.h"
30
31
32
/* ------------------------------------------------------------------------ */
33
/*
34
 *   Statics 
35
 */
36
/* metaclass registration object */
37
static CVmMetaclassAnonFn metaclass_reg_obj;
38
CVmMetaclass *CVmObjAnonFn::metaclass_reg_ = &metaclass_reg_obj;
39
40
41
/* ------------------------------------------------------------------------ */
42
/*
43
 *   create from stack arguments
44
 */
45
vm_obj_id_t CVmObjAnonFn::create_from_stack(VMG_ const uchar **pc_ptr,
46
                                            uint argc)
47
{
48
    vm_obj_id_t id;
49
    vm_val_t funcptr;
50
    CVmObjAnonFn *new_obj;
51
    uint idx;
52
    
53
    /* at least one argument is required (the function pointer) */
54
    if (argc < 1)
55
        err_throw(VMERR_WRONG_NUM_OF_ARGS);
56
57
    /* retrieve our function pointer argument */
58
    G_stk->pop(&funcptr);
59
    if (funcptr.typ != VM_FUNCPTR)
60
        err_throw(VMERR_FUNCPTR_VAL_REQD);
61
62
    /* create the new object */
63
    id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
64
65
    /* create the new object, giving it one slot per constructor argument */
66
    new_obj = new (vmg_ id) CVmObjAnonFn(vmg_ argc);
67
68
    /* set the first element to our function pointer */
69
    new_obj->set_element(0, &funcptr);
70
71
    /* set the remaining elements to the context objects */
72
    for (idx = 1 ; idx < argc ; ++idx)
73
    {
74
        vm_val_t val;
75
        
76
        /* pop this value */
77
        G_stk->pop(&val);
78
79
        /* set the element */
80
        new_obj->set_element(idx, &val);
81
    }
82
83
    /* return the new object ID */
84
    return id;
85
}
86
87
/* ------------------------------------------------------------------------ */
88
/*
89
 *   Get a property 
90
 */
91
int CVmObjAnonFn::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
92
                           vm_obj_id_t self, vm_obj_id_t *source_obj,
93
                           uint *argc)
94
{
95
    /* 
96
     *   if the property is the special ObjectCallProp property, return
97
     *   our first element 
98
     */
99
    if (prop == G_predef->obj_call_prop && prop != VM_INVALID_PROP)
100
    {
101
        static CVmNativeCodeDesc desc(0);
102
103
        /* check for arguments */
104
        if (get_prop_check_argc(val, argc, &desc))
105
            return TRUE;
106
107
        /* return our first element */
108
        get_element(0, val);
109
110
        /* we're the source object */
111
        *source_obj = self;
112
113
        /* success */
114
        return TRUE;
115
    }
116
117
    /* it's not one of our own - inherit default handling */
118
    return CVmObjVector::get_prop(vmg_ prop, val, self, source_obj, argc);
119
}