| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/VMBIFTAD.CPP,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 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 | vmbift3.cpp - T3 VM system interface function set |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 04/05/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "utf8.h" |
| 27 | #include "vmbif.h" |
| 28 | #include "vmbift3.h" |
| 29 | #include "vmstack.h" |
| 30 | #include "vmerr.h" |
| 31 | #include "vmerrnum.h" |
| 32 | #include "vmglob.h" |
| 33 | #include "vmpool.h" |
| 34 | #include "vmobj.h" |
| 35 | #include "vmrun.h" |
| 36 | #include "vmstr.h" |
| 37 | #include "vmvsn.h" |
| 38 | #include "vmimage.h" |
| 39 | #include "vmlst.h" |
| 40 | #include "vmtobj.h" |
| 41 | #include "vmfunc.h" |
| 42 | #include "vmpredef.h" |
| 43 | #include "vmsrcf.h" |
| 44 | #include "charmap.h" |
| 45 | #include "vmlookup.h" |
| 46 | #include "vmimport.h" |
| 47 | #include "vmmeta.h" |
| 48 | #include "vmfref.h" |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * run the garbage collector |
| 53 | */ |
| 54 | void CVmBifT3::run_gc(VMG_ uint argc) |
| 55 | { |
| 56 | /* no arguments are allowed */ |
| 57 | check_argc(vmg_ argc, 0); |
| 58 | |
| 59 | /* run the garbage collector */ |
| 60 | G_obj_table->gc_full(vmg0_); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * set the SAY instruction's handler function |
| 65 | */ |
| 66 | #define SETSAY_NO_FUNC 1 |
| 67 | #define SETSAY_NO_METHOD 2 |
| 68 | void CVmBifT3::set_say(VMG_ uint argc) |
| 69 | { |
| 70 | vm_val_t *arg = G_stk->get(0); |
| 71 | vm_val_t val; |
| 72 | |
| 73 | /* one argument is required */ |
| 74 | check_argc(vmg_ argc, 1); |
| 75 | |
| 76 | /* check to see if we're setting the default display method */ |
| 77 | if (arg->typ == VM_PROP |
| 78 | || (arg->typ == VM_INT && arg->val.intval == SETSAY_NO_METHOD)) |
| 79 | { |
| 80 | vm_prop_id_t prop; |
| 81 | |
| 82 | /* |
| 83 | * the return value is the old property pointer (or |
| 84 | * SETSAY_NO_METHOD if there was no valid property set previously) |
| 85 | */ |
| 86 | prop = G_interpreter->get_say_method(); |
| 87 | if (prop != VM_INVALID_PROP) |
| 88 | retval_prop(vmg_ prop); |
| 89 | else |
| 90 | retval_int(vmg_ SETSAY_NO_METHOD); |
| 91 | |
| 92 | /* get the new value */ |
| 93 | G_stk->pop(&val); |
| 94 | |
| 95 | /* if it's SETSAY_NO_METHOD, set it to the invalid prop ID */ |
| 96 | if (val.typ == VM_INT) |
| 97 | val.set_propid(VM_INVALID_PROP); |
| 98 | |
| 99 | /* set the method */ |
| 100 | G_interpreter->set_say_method(val.val.prop); |
| 101 | } |
| 102 | else if (arg->typ == VM_FUNCPTR |
| 103 | || arg->typ == VM_OBJ |
| 104 | || arg->typ == VM_BIFPTR |
| 105 | || (arg->typ == VM_INT && arg->val.intval == SETSAY_NO_FUNC)) |
| 106 | { |
| 107 | /* |
| 108 | * the return value is the old function (or SETSAY_NO_FUNC if the |
| 109 | * old function was nil) |
| 110 | */ |
| 111 | G_interpreter->get_say_func(&val); |
| 112 | if (val.typ != VM_NIL) |
| 113 | retval(vmg_ &val); |
| 114 | else |
| 115 | retval_int(vmg_ SETSAY_NO_FUNC); |
| 116 | |
| 117 | /* get the new function value */ |
| 118 | G_stk->pop(&val); |
| 119 | |
| 120 | /* if it's SETSAY_NO_FUNC, set the function to nil */ |
| 121 | if (val.typ == VM_INT) |
| 122 | val.set_nil(); |
| 123 | |
| 124 | /* set the new function */ |
| 125 | G_interpreter->set_say_func(vmg_ &val); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | /* invalid type */ |
| 130 | err_throw(VMERR_BAD_TYPE_BIF); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * get the VM version number |
| 136 | */ |
| 137 | void CVmBifT3::get_vm_vsn(VMG_ uint argc) |
| 138 | { |
| 139 | /* no arguments are allowed */ |
| 140 | check_argc(vmg_ argc, 0); |
| 141 | |
| 142 | /* set the integer return value */ |
| 143 | retval_int(vmg_ T3VM_VSN_NUMBER); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * get the VM identification string |
| 148 | */ |
| 149 | void CVmBifT3::get_vm_id(VMG_ uint argc) |
| 150 | { |
| 151 | /* no arguments are allowed */ |
| 152 | check_argc(vmg_ argc, 0); |
| 153 | |
| 154 | /* set the integer return value */ |
| 155 | retval_str(vmg_ T3VM_IDENTIFICATION); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | * get the VM banner string |
| 161 | */ |
| 162 | void CVmBifT3::get_vm_banner(VMG_ uint argc) |
| 163 | { |
| 164 | /* no arguments are allowed */ |
| 165 | check_argc(vmg_ argc, 0); |
| 166 | |
| 167 | /* return the string */ |
| 168 | retval_str(vmg_ T3VM_BANNER_STRING); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * get the 'preinit' status - true if preinit, nil if normal |
| 173 | */ |
| 174 | void CVmBifT3::get_vm_preinit_mode(VMG_ uint argc) |
| 175 | { |
| 176 | /* no arguments allowed */ |
| 177 | check_argc(vmg_ argc, 0); |
| 178 | |
| 179 | /* return the preinit mode */ |
| 180 | retval_int(vmg_ G_preinit_mode); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * get the runtime symbol table |
| 185 | */ |
| 186 | void CVmBifT3::get_global_symtab(VMG_ uint argc) |
| 187 | { |
| 188 | /* check arguments */ |
| 189 | check_argc_range(vmg_ argc, 0, 1); |
| 190 | |
| 191 | /* if there's an argument, it specifies which table to retrieve */ |
| 192 | int which = 1; |
| 193 | if (argc >= 1) |
| 194 | which = pop_int_val(vmg0_); |
| 195 | |
| 196 | /* return the desired table */ |
| 197 | switch (which) |
| 198 | { |
| 199 | case 1: |
| 200 | /* return the loader's symbol table object, if any */ |
| 201 | retval_obj(vmg_ G_image_loader->get_reflection_symtab()); |
| 202 | break; |
| 203 | |
| 204 | case 2: |
| 205 | /* return the macro table, if any */ |
| 206 | retval_obj(vmg_ G_image_loader->get_reflection_macros()); |
| 207 | break; |
| 208 | |
| 209 | case 3: |
| 210 | /* other values are allowed but simply return nil */ |
| 211 | retval_nil(vmg0_); |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * allocate a new property ID |
| 218 | */ |
| 219 | void CVmBifT3::alloc_new_prop(VMG_ uint argc) |
| 220 | { |
| 221 | /* check arguments */ |
| 222 | check_argc(vmg_ argc, 0); |
| 223 | |
| 224 | /* allocate and return a new property ID */ |
| 225 | retval_prop(vmg_ G_image_loader->alloc_new_prop(vmg0_)); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * get a stack trace |
| 230 | */ |
| 231 | #define T3_GST_LOCALS 0x0001 |
| 232 | #define T3_GST_FREFS 0x0002 |
| 233 | void CVmBifT3::get_stack_trace(VMG_ uint argc) |
| 234 | { |
| 235 | int single_level = 0; |
| 236 | int level; |
| 237 | vm_val_t *fp; |
| 238 | vm_val_t lst_val; |
| 239 | CVmObjList *lst; |
| 240 | const uchar *entry_addr; |
| 241 | ulong method_ofs; |
| 242 | vm_val_t stack_info_cls; |
| 243 | int want_named_args = FALSE; |
| 244 | int want_locals = FALSE; |
| 245 | int want_frefs = FALSE; |
| 246 | int flags = 0; |
| 247 | const vm_rcdesc *rc; |
| 248 | |
| 249 | /* check arguments */ |
| 250 | check_argc_range(vmg_ argc, 0, 2); |
| 251 | |
| 252 | /* get the imported stack information class */ |
| 253 | stack_info_cls.set_obj(G_predef->stack_info_cls); |
| 254 | if (stack_info_cls.val.obj == VM_INVALID_OBJ) |
| 255 | { |
| 256 | /* |
| 257 | * there's no stack information class - we can't return any |
| 258 | * meaningful information, so just return nil |
| 259 | */ |
| 260 | retval_nil(vmg0_); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * look up T3StackInfo.construct() to determine how many arguments it |
| 266 | * wants |
| 267 | */ |
| 268 | { |
| 269 | int min_args, opt_args, varargs; |
| 270 | if (vm_objp(vmg_ stack_info_cls.val.obj)->get_prop_interface( |
| 271 | vmg_ stack_info_cls.val.obj, G_predef->obj_construct, |
| 272 | min_args, opt_args, varargs)) |
| 273 | { |
| 274 | /* check to see how many extra arguments they want */ |
| 275 | want_named_args = (min_args + opt_args >= 7 || varargs); |
| 276 | want_locals = (min_args + opt_args >= 8 || varargs); |
| 277 | want_frefs = (min_args + opt_args >= 9 || varargs); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* check to see if we're fetching a single level or the full trace */ |
| 282 | if (argc >= 1) |
| 283 | { |
| 284 | /* |
| 285 | * Get the single level, and adjust to a 0 base. If the level is |
| 286 | * nil, we're still getting all levels. |
| 287 | */ |
| 288 | if (G_stk->get(0)->typ == VM_NIL) |
| 289 | { |
| 290 | /* it's nil - get all levels */ |
| 291 | G_stk->discard(); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | /* get the level number */ |
| 296 | single_level = pop_int_val(vmg0_); |
| 297 | |
| 298 | /* make sure it's in range */ |
| 299 | if (single_level <= 0) |
| 300 | err_throw(VMERR_BAD_VAL_BIF); |
| 301 | |
| 302 | /* we won't need a return list */ |
| 303 | lst_val.set_obj_or_nil(VM_INVALID_OBJ); |
| 304 | lst = 0; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* get the flags argument, if present */ |
| 309 | if (argc >= 2) |
| 310 | flags = pop_int_val(vmg0_); |
| 311 | |
| 312 | /* if we're not doing a single level, we need a list for the result */ |
| 313 | if (!single_level) |
| 314 | { |
| 315 | /* |
| 316 | * We're returning a full list, so we need to allocate the list for |
| 317 | * the return value. First, count stack levels to see how big a |
| 318 | * list we'll need. |
| 319 | */ |
| 320 | fp = G_interpreter->get_frame_ptr(); |
| 321 | entry_addr = G_interpreter->get_entry_ptr(); |
| 322 | method_ofs = G_interpreter->get_method_ofs(); |
| 323 | for (level = 0 ; fp != 0 ; |
| 324 | fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp), ++level) |
| 325 | { |
| 326 | /* add an extra level for each system call */ |
| 327 | if (method_ofs == 0 && entry_addr != 0) |
| 328 | ++level; |
| 329 | |
| 330 | /* get the return address */ |
| 331 | entry_addr = |
| 332 | G_interpreter->get_enclosing_entry_ptr_from_frame(vmg_ fp); |
| 333 | method_ofs = G_interpreter->get_return_ofs_from_frame(vmg_ fp); |
| 334 | } |
| 335 | |
| 336 | /* create the list */ |
| 337 | lst_val.set_obj(CVmObjList::create(vmg_ FALSE, level)); |
| 338 | lst = (CVmObjList *)vm_objp(vmg_ lst_val.val.obj); |
| 339 | |
| 340 | /* |
| 341 | * we create other objects while building this list, so the gc |
| 342 | * could run - clear the list to ensure it contains valid data |
| 343 | */ |
| 344 | lst->cons_clear(); |
| 345 | |
| 346 | /* protect the list from garbage collection while we work */ |
| 347 | G_stk->push(&lst_val); |
| 348 | |
| 349 | /* flag that we're doing the whole stack */ |
| 350 | single_level = -1; |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | /* adjust the level to a 0-based index */ |
| 355 | single_level -= 1; |
| 356 | } |
| 357 | |
| 358 | /* set up at the current function */ |
| 359 | fp = G_interpreter->get_frame_ptr(); |
| 360 | entry_addr = G_interpreter->get_entry_ptr(); |
| 361 | method_ofs = G_interpreter->get_method_ofs(); |
| 362 | rc = 0; |
| 363 | |
| 364 | /* traverse the frames */ |
| 365 | for (level = 0 ; fp != 0 ; ++level) |
| 366 | { |
| 367 | int fr_argc; |
| 368 | int i; |
| 369 | vm_obj_id_t def_obj; |
| 370 | vm_val_t info_self; |
| 371 | vm_val_t info_func; |
| 372 | vm_val_t info_obj; |
| 373 | vm_val_t info_prop; |
| 374 | vm_val_t info_args; |
| 375 | vm_val_t info_locals; |
| 376 | vm_val_t info_srcloc; |
| 377 | vm_val_t info_frameref; |
| 378 | CVmObjList *arglst; |
| 379 | vm_val_t ele; |
| 380 | CVmFuncPtr func_ptr; |
| 381 | int gc_cnt = 0; |
| 382 | int info_argc = 0; |
| 383 | |
| 384 | /* if we're looking for a single level, and this isn't it, skip it */ |
| 385 | if (single_level >= 0 && level != single_level) |
| 386 | goto done_with_level; |
| 387 | |
| 388 | /* |
| 389 | * start with the information values to nil - we'll set the |
| 390 | * appropriate ones when we find out what we have |
| 391 | */ |
| 392 | info_func.set_nil(); |
| 393 | info_obj.set_nil(); |
| 394 | info_prop.set_nil(); |
| 395 | info_self.set_nil(); |
| 396 | info_locals.set_nil(); |
| 397 | info_frameref.set_nil(); |
| 398 | |
| 399 | /* get the number of arguments to the function in this frame */ |
| 400 | fr_argc = G_interpreter->get_argc_from_frame(vmg_ fp); |
| 401 | |
| 402 | /* set up a function pointer for the method's entry address */ |
| 403 | func_ptr.set(entry_addr); |
| 404 | |
| 405 | /* get the current frame's defining object */ |
| 406 | def_obj = G_interpreter->get_defining_obj_from_frame(vmg_ fp); |
| 407 | |
| 408 | /* check for special method offsets */ |
| 409 | switch (method_ofs) |
| 410 | { |
| 411 | case VMRUN_RET_OP: |
| 412 | /* the real return address is one past the last argument */ |
| 413 | method_ofs = G_interpreter->get_param_from_frame(vmg_ fp, argc) |
| 414 | ->val.intval; |
| 415 | break; |
| 416 | |
| 417 | case VMRUN_RET_OP_ASILCL: |
| 418 | /* the real return address is two past the last argument */ |
| 419 | method_ofs = G_interpreter->get_param_from_frame(vmg_ fp, argc+1) |
| 420 | ->val.intval; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | /* determine whether it's an object.prop or a function call */ |
| 425 | if (method_ofs == 0) |
| 426 | { |
| 427 | /* |
| 428 | * A zero method offset indicates a recursive VM invocation |
| 429 | * from a native function. Presume we have no information on |
| 430 | * the caller. |
| 431 | */ |
| 432 | info_self.set_nil(); |
| 433 | fr_argc = 0; |
| 434 | |
| 435 | /* check for a native caller context */ |
| 436 | if (rc != 0) |
| 437 | { |
| 438 | /* check which kind of native caller we have */ |
| 439 | if (rc->bifptr.typ != VM_NIL) |
| 440 | { |
| 441 | /* we have a built-in function at this level */ |
| 442 | info_func = rc->bifptr; |
| 443 | } |
| 444 | else if (rc->self.typ != VM_NIL) |
| 445 | { |
| 446 | /* it's an intrinsic class method - get the 'self' */ |
| 447 | info_obj = info_self = rc->self; |
| 448 | |
| 449 | /* get the metaclass */ |
| 450 | CVmMetaclass *mc; |
| 451 | switch (info_obj.typ) |
| 452 | { |
| 453 | case VM_OBJ: |
| 454 | /* get the metaclass from the object */ |
| 455 | mc = vm_objp(vmg_ info_obj.val.obj) |
| 456 | ->get_metaclass_reg(); |
| 457 | break; |
| 458 | |
| 459 | case VM_LIST: |
| 460 | /* list constant - use the List metaclass */ |
| 461 | mc = CVmObjList::metaclass_reg_; |
| 462 | break; |
| 463 | |
| 464 | case VM_SSTRING: |
| 465 | /* string constant - use the String metaclass */ |
| 466 | mc = CVmObjString::metaclass_reg_; |
| 467 | break; |
| 468 | |
| 469 | default: |
| 470 | /* other types don't have metaclasses */ |
| 471 | mc = 0; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | /* get the registration table entry */ |
| 476 | vm_meta_entry_t *me = |
| 477 | mc == 0 ? 0 : |
| 478 | G_meta_table->get_entry_from_reg(mc->get_reg_idx()); |
| 479 | |
| 480 | /* get the metaclass and property from the entry */ |
| 481 | if (me != 0) |
| 482 | { |
| 483 | /* set 'obj' to the IntrinsicClass object */ |
| 484 | info_obj.set_obj(me->class_obj_); |
| 485 | |
| 486 | /* get the property ID */ |
| 487 | info_prop.set_propid(me->xlat_func(rc->method_idx)); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | else if (def_obj == VM_INVALID_OBJ) |
| 493 | { |
| 494 | /* there's no defining object, so this is a function call */ |
| 495 | func_ptr.get_fnptr(vmg_ &info_func); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | /* it's an object.prop invocation */ |
| 500 | info_obj.set_obj(def_obj); // $$$ walk up to base modified obj? |
| 501 | info_prop.set_propid( |
| 502 | G_interpreter->get_target_prop_from_frame(vmg_ fp)); |
| 503 | |
| 504 | /* get the 'self' in this frame */ |
| 505 | info_self.set_obj(G_interpreter->get_self_from_frame(vmg_ fp)); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * build the argument list and source location, except for system |
| 510 | * routines |
| 511 | */ |
| 512 | if (method_ofs != 0 || rc != 0) |
| 513 | { |
| 514 | /* allocate a list object to store the argument list */ |
| 515 | int ac = (rc != 0 ? rc->argc : fr_argc); |
| 516 | info_args.set_obj(CVmObjList::create(vmg_ FALSE, ac)); |
| 517 | arglst = (CVmObjList *)vm_objp(vmg_ info_args.val.obj); |
| 518 | |
| 519 | /* push the argument list for gc protection */ |
| 520 | G_stk->push(&info_args); |
| 521 | ++gc_cnt; |
| 522 | |
| 523 | /* build the argument list */ |
| 524 | for (i = 0 ; i < ac ; ++i) |
| 525 | { |
| 526 | /* add this element to the argument list */ |
| 527 | const vm_val_t *v = |
| 528 | (rc != 0 |
| 529 | ? rc->argp - i |
| 530 | : G_interpreter->get_param_from_frame(vmg_ fp, i)); |
| 531 | arglst->cons_set_element(i, v); |
| 532 | } |
| 533 | |
| 534 | /* get the source location */ |
| 535 | get_source_info(vmg_ entry_addr, method_ofs, &info_srcloc); |
| 536 | |
| 537 | /* |
| 538 | * if they want locals, and this isn't a recursive native |
| 539 | * caller, retrieve them |
| 540 | */ |
| 541 | if (rc == 0 |
| 542 | && (((flags & T3_GST_LOCALS) != 0 && want_locals) |
| 543 | || ((flags & T3_GST_FREFS) != 0 && want_frefs))) |
| 544 | { |
| 545 | /* get the locals */ |
| 546 | get_stack_locals(vmg_ fp, entry_addr, method_ofs, |
| 547 | (flags & T3_GST_LOCALS) != 0 && want_locals |
| 548 | ? &info_locals : 0, |
| 549 | (flags & T3_GST_FREFS) != 0 && want_frefs |
| 550 | ? &info_frameref : 0); |
| 551 | |
| 552 | /* |
| 553 | * that leaves the LookupTable and StackFrameDesc on the |
| 554 | * stack, so note that we need to discard the stack level |
| 555 | * when we're done with it |
| 556 | */ |
| 557 | if (info_locals.typ == VM_OBJ) |
| 558 | ++gc_cnt; |
| 559 | if (info_frameref.typ == VM_OBJ) |
| 560 | ++gc_cnt; |
| 561 | } |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | /* |
| 566 | * it's a system routine - no argument information is |
| 567 | * available, so return nil rather than an empty list to to |
| 568 | * indicate the absence |
| 569 | */ |
| 570 | info_args.set_nil(); |
| 571 | |
| 572 | /* there's obviously no source location for system code */ |
| 573 | info_srcloc.set_nil(); |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * We have all of the information on this level now, so create the |
| 578 | * information object for the level. This is an object of the |
| 579 | * exported stack-info class, which is a TadsObject type. |
| 580 | */ |
| 581 | |
| 582 | /* start with the original complement of arguments */ |
| 583 | info_argc = 7; |
| 584 | |
| 585 | /* |
| 586 | * if we have a modern T3StackInfo object, push the locals, |
| 587 | * named argument elements, and frame reference object |
| 588 | */ |
| 589 | if (want_frefs) |
| 590 | { |
| 591 | G_stk->push(&info_frameref); |
| 592 | ++info_argc; |
| 593 | } |
| 594 | if (want_named_args) |
| 595 | { |
| 596 | /* |
| 597 | * the constructor has a slot for named arguments - push either |
| 598 | * a table or nil, depending... |
| 599 | */ |
| 600 | vm_val_t *argp; |
| 601 | const uchar *t = 0; |
| 602 | |
| 603 | /* if the flags request locals, retrieve the named arguments */ |
| 604 | if ((flags & T3_GST_LOCALS) != 0) |
| 605 | t = CVmRun::get_named_args_from_frame(vmg_ fp, &argp); |
| 606 | |
| 607 | /* |
| 608 | * if we do in fact have named arguments, build a lookup table |
| 609 | * copy and push it; otherwise just push nil |
| 610 | */ |
| 611 | if (t != 0) |
| 612 | { |
| 613 | /* get the number of table entries */ |
| 614 | int n = osrp2(t); |
| 615 | t += 2; |
| 616 | |
| 617 | /* create a lookup table for the arguments */ |
| 618 | G_stk->push()->set_obj(CVmObjLookupTable::create( |
| 619 | vmg_ FALSE, n <= 8 ? 8 : n <= 32 ? 32 : 64, n)); |
| 620 | CVmObjLookupTable *lt = (CVmObjLookupTable *)vm_objp( |
| 621 | vmg_ G_stk->get(0)->val.obj); |
| 622 | |
| 623 | /* |
| 624 | * Populate the lookup table with the named arguments. The |
| 625 | * compiler builds the table in the order pushed, which is |
| 626 | * right to left. Lookup tables preserve the order in |
| 627 | * which elements are added, and reflect this order in key |
| 628 | * lists, so to that extent the order of building the |
| 629 | * lookup table matters. For readability of the generated |
| 630 | * list, in case it's presented to the user, build the |
| 631 | * table in left-to-right order, which is the reverse of |
| 632 | * the table order in the bytecode table. |
| 633 | */ |
| 634 | argp += n - 1; |
| 635 | for (int i = (n-1)*2 ; i >= 0 ; i -= 2, --argp) |
| 636 | { |
| 637 | /* get the name pointer and length from the index */ |
| 638 | uint ofs = osrp2(t + i), nxtofs = osrp2(t + i + 2); |
| 639 | const char *name = (const char *)t + ofs; |
| 640 | size_t len = nxtofs - ofs; |
| 641 | |
| 642 | /* create a string from the name */ |
| 643 | vm_val_t str; |
| 644 | str.set_obj(CVmObjString::create(vmg_ FALSE, name, len)); |
| 645 | |
| 646 | /* add it to the table */ |
| 647 | lt->add_entry(vmg_ &str, argp); |
| 648 | } |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | /* there are no named arguments - push nil */ |
| 653 | G_stk->push()->set_nil(); |
| 654 | } |
| 655 | |
| 656 | /* count the argument */ |
| 657 | ++info_argc; |
| 658 | } |
| 659 | if (want_locals) |
| 660 | { |
| 661 | G_stk->push(&info_locals); |
| 662 | ++info_argc; |
| 663 | } |
| 664 | |
| 665 | /* push the rest of the arguments */ |
| 666 | G_stk->push(&info_srcloc); |
| 667 | G_stk->push(&info_args); |
| 668 | G_stk->push(&info_self); |
| 669 | G_stk->push(&info_prop); |
| 670 | G_stk->push(&info_obj); |
| 671 | G_stk->push(&info_func); |
| 672 | G_stk->push(&stack_info_cls); |
| 673 | ele.set_obj(CVmObjTads::create_from_stack(vmg_ 0, info_argc)); |
| 674 | |
| 675 | /* discard the gc protection items */ |
| 676 | G_stk->discard(gc_cnt); |
| 677 | |
| 678 | /* |
| 679 | * if we're fetching a single level, this is it - return the new |
| 680 | * stack info object and we're done |
| 681 | */ |
| 682 | if (single_level >= 0) |
| 683 | { |
| 684 | /* return the single level object */ |
| 685 | retval_obj(vmg_ ele.val.obj); |
| 686 | |
| 687 | /* we're done */ |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | /* add the new element to our list */ |
| 692 | lst->cons_set_element(level, &ele); |
| 693 | |
| 694 | done_with_level: |
| 695 | /* |
| 696 | * If this is a system call level, and we're not in debug mode, |
| 697 | * this recursive frame contains the entry address for the caller, |
| 698 | * but not the calling byte-code address. Stay on the current |
| 699 | * level in this case. |
| 700 | */ |
| 701 | if (method_ofs == 0 && entry_addr != 0) |
| 702 | { |
| 703 | /* |
| 704 | * This is a recursive caller, and we have a valid entry |
| 705 | * address for the prior frame. Stay in the current frame, and |
| 706 | * retrieve the actual return address from the calling frame. |
| 707 | */ |
| 708 | if (rc != 0) |
| 709 | { |
| 710 | /* get the actual return address from the recursive context */ |
| 711 | method_ofs = rc->get_return_addr() - entry_addr; |
| 712 | |
| 713 | /* |
| 714 | * we're now in the bytecode part of the frame, so forget |
| 715 | * the recursive context |
| 716 | */ |
| 717 | rc = 0; |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | /* no recursive context - use a fake return address */ |
| 722 | method_ofs = G_interpreter->get_funchdr_size(); |
| 723 | } |
| 724 | } |
| 725 | else |
| 726 | { |
| 727 | /* move up to the enclosing frame */ |
| 728 | entry_addr = |
| 729 | G_interpreter->get_enclosing_entry_ptr_from_frame(vmg_ fp); |
| 730 | method_ofs = G_interpreter->get_return_ofs_from_frame(vmg_ fp); |
| 731 | rc = G_interpreter->get_rcdesc_from_frame(vmg_ fp); |
| 732 | fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /* return the list */ |
| 737 | retval_obj(vmg_ lst_val.val.obj); |
| 738 | |
| 739 | /* discard our gc protection */ |
| 740 | G_stk->discard(); |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Get the local variable table for a given stack level. We'll fill in |
| 745 | * 'retval' with a LooupTable object containing the local values keyed by |
| 746 | * symbol name, or nil if we can't find the locals. We'll also push the |
| 747 | * object onto the stack for gc protection, so the caller must pop it when |
| 748 | * done with it. |
| 749 | */ |
| 750 | void CVmBifT3::get_stack_locals(VMG_ vm_val_t *fp, |
| 751 | const uchar *entry_addr, ulong method_ofs, |
| 752 | vm_val_t *local_tab, vm_val_t *frameref_obj) |
| 753 | { |
| 754 | /* presume we won't be able to find source information for the location */ |
| 755 | if (local_tab != 0) |
| 756 | local_tab->set_nil(); |
| 757 | if (frameref_obj != 0) |
| 758 | frameref_obj->set_nil(); |
| 759 | |
| 760 | /* we haven't found any relevant frames yet */ |
| 761 | int best = 0; |
| 762 | |
| 763 | /* set up a debug table pointer for the function or method */ |
| 764 | CVmDbgTablePtr dp; |
| 765 | if (dp.set(entry_addr)) |
| 766 | { |
| 767 | /* search for the innermost frame containing the method offset */ |
| 768 | int frame_cnt = dp.get_frame_count(vmg0_); |
| 769 | for (int i = 1 ; i <= frame_cnt ; ++i) |
| 770 | { |
| 771 | /* get this frame */ |
| 772 | CVmDbgFramePtr fp; |
| 773 | dp.set_frame_ptr(vmg_ &fp, i); |
| 774 | |
| 775 | /* check to see if this contains method_ofs */ |
| 776 | uint start_ofs = fp.get_start_ofs(vmg0_); |
| 777 | uint end_ofs = fp.get_end_ofs(vmg0_); |
| 778 | if (method_ofs >= start_ofs && method_ofs <= end_ofs) |
| 779 | { |
| 780 | /* |
| 781 | * This is a relevant frame. If we don't have any frames |
| 782 | * yet, or this one is nested within the best we've found |
| 783 | * so far, this is the best frame so far. |
| 784 | */ |
| 785 | if (best == 0 || fp.is_nested_in(vmg_ &dp, best)) |
| 786 | best = i; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /* get the frame-reference, if the caller wants one */ |
| 792 | if (frameref_obj != 0) |
| 793 | { |
| 794 | /* get the slot */ |
| 795 | vm_val_t *fro = G_interpreter->get_frameref_slot(vmg_ fp); |
| 796 | |
| 797 | /* if there's not already a StackFrameRef object there, create one */ |
| 798 | if (fro->typ == VM_NIL) |
| 799 | { |
| 800 | /* create the StackFrameRef and store it in the stack slot */ |
| 801 | fro->set_obj(CVmObjFrameRef::create(vmg_ fp, entry_addr)); |
| 802 | } |
| 803 | |
| 804 | /* create the StackFrameDesc to return to the caller */ |
| 805 | frameref_obj->set_obj(CVmObjFrameDesc::create( |
| 806 | vmg_ fro->val.obj, best, method_ofs)); |
| 807 | |
| 808 | /* push it for gc protection */ |
| 809 | G_stk->push(frameref_obj); |
| 810 | } |
| 811 | |
| 812 | /* if they didn't want the local variable table, we're done */ |
| 813 | if (local_tab == 0) |
| 814 | return; |
| 815 | |
| 816 | /* create a lookup table for the locals */ |
| 817 | local_tab->set_obj(CVmObjLookupTable::create(vmg_ FALSE, 32, 64)); |
| 818 | CVmObjLookupTable *t = |
| 819 | (CVmObjLookupTable *)vm_objp(vmg_ local_tab->val.obj); |
| 820 | |
| 821 | /* save it on the stack for gc protection */ |
| 822 | G_stk->push(local_tab); |
| 823 | |
| 824 | /* if we didn't find a best frame, there are no locals to retrieve */ |
| 825 | if (best == 0) |
| 826 | return; |
| 827 | |
| 828 | /* set up a pointer to our winning frame */ |
| 829 | CVmDbgFramePtr dfp; |
| 830 | dp.set_frame_ptr(vmg_ &dfp, best); |
| 831 | |
| 832 | /* walk up the list of frames from the innermost to outermost */ |
| 833 | for (;;) |
| 834 | { |
| 835 | /* set up a pointer to the first symbol */ |
| 836 | CVmDbgFrameSymPtr symp; |
| 837 | dfp.set_first_sym_ptr(vmg_ &symp); |
| 838 | |
| 839 | /* scan this frame's local symbol list */ |
| 840 | int sym_cnt = dfp.get_sym_count(); |
| 841 | for (int i = 0 ; i < sym_cnt ; ++i, symp.inc(vmg0_)) |
| 842 | { |
| 843 | vm_val_t name, val; |
| 844 | |
| 845 | /* get the symbol name */ |
| 846 | symp.get_str_val(vmg_ &name); |
| 847 | |
| 848 | /* |
| 849 | * if this symbol is already in the table, skip it - we work |
| 850 | * from inner to outer frames, and inner frames hide symbols in |
| 851 | * outer frames |
| 852 | */ |
| 853 | if (t->index_check(vmg_ &val, &name)) |
| 854 | continue; |
| 855 | |
| 856 | /* get the value from the frame */ |
| 857 | G_interpreter->get_local_from_frame(vmg_ &val, fp, &symp); |
| 858 | |
| 859 | /* add this to the lookup table */ |
| 860 | t->add_entry(vmg_ &name, &val); |
| 861 | } |
| 862 | |
| 863 | /* get the enclosing frame - 0 means no parent */ |
| 864 | int parent = dfp.get_enclosing_frame(); |
| 865 | if (parent == 0) |
| 866 | break; |
| 867 | |
| 868 | /* set up dfp to point to the parent frame */ |
| 869 | dp.set_frame_ptr(vmg_ &dfp, parent); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * Get the source file information for a given code pool offset. If debug |
| 875 | * records aren't available for the given location, returns nil. Returns |
| 876 | * a list containing the source file information: the first element is a |
| 877 | * string giving the name of the file, and the second element is an |
| 878 | * integer giving the line number in the file. Returns nil if no source |
| 879 | * information is available for the given byte code location. |
| 880 | */ |
| 881 | void CVmBifT3::get_source_info(VMG_ const uchar *entry_addr, ulong method_ofs, |
| 882 | vm_val_t *retval) |
| 883 | { |
| 884 | CVmFuncPtr func_ptr; |
| 885 | CVmDbgLinePtr line_ptr; |
| 886 | const uchar *stm_start, *stm_end; |
| 887 | CVmObjList *lst; |
| 888 | vm_val_t ele; |
| 889 | CVmSrcfEntry *srcf; |
| 890 | CVmObjString *str; |
| 891 | const char *fname; |
| 892 | size_t map_len; |
| 893 | |
| 894 | /* presume we won't be able to find source information for the location */ |
| 895 | retval->set_nil(); |
| 896 | |
| 897 | /* set up a debug table pointer for the function or method */ |
| 898 | func_ptr.set(entry_addr); |
| 899 | |
| 900 | /* get the debug information for the given location */ |
| 901 | if (!CVmRun::get_stm_bounds(vmg_ &func_ptr, method_ofs, |
| 902 | &line_ptr, &stm_start, &stm_end)) |
| 903 | { |
| 904 | /* no source information available - return failure */ |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | /* get the source file record - if we can't find it, return failure */ |
| 909 | srcf = (G_srcf_table != 0 |
| 910 | ? G_srcf_table->get_entry(line_ptr.get_source_id()) : 0); |
| 911 | if (srcf == 0) |
| 912 | return; |
| 913 | |
| 914 | /* |
| 915 | * Create a list for the return value. The return list has two |
| 916 | * elements: the name of the source file containing this code, and the |
| 917 | * line number in the file. |
| 918 | */ |
| 919 | retval->set_obj(CVmObjList::create(vmg_ FALSE, 2)); |
| 920 | lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj); |
| 921 | |
| 922 | /* clear the list, in case gc runs during construction */ |
| 923 | lst->cons_clear(); |
| 924 | |
| 925 | /* push the list for gc protection */ |
| 926 | G_stk->push(retval); |
| 927 | |
| 928 | /* get the filename string */ |
| 929 | fname = srcf->get_name(); |
| 930 | |
| 931 | /* |
| 932 | * determine how long the string will be when translated to utf8 from |
| 933 | * the local filename character set |
| 934 | */ |
| 935 | map_len = G_cmap_from_fname->map_str(0, 0, fname); |
| 936 | |
| 937 | /* |
| 938 | * create a string value to hold the filename, and store it in the |
| 939 | * first element of the return list (note that this automatically |
| 940 | * protects the new string from garbage collection, by virtue of the |
| 941 | * list referencing the string and the list itself being protected) |
| 942 | */ |
| 943 | ele.set_obj(CVmObjString::create(vmg_ FALSE, map_len)); |
| 944 | lst->cons_set_element(0, &ele); |
| 945 | |
| 946 | /* map the string into the buffer we allocated for it */ |
| 947 | str = (CVmObjString *)vm_objp(vmg_ ele.val.obj); |
| 948 | G_cmap_from_fname->map_str(str->cons_get_buf(), map_len, fname); |
| 949 | |
| 950 | /* set the second element of the list to the source line number */ |
| 951 | ele.set_int(line_ptr.get_source_line()); |
| 952 | lst->cons_set_element(1, &ele); |
| 953 | |
| 954 | /* discard our gc protection */ |
| 955 | G_stk->discard(); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | /* |
| 960 | * Look up a named argument. If 'mandatory' is set, we throw an error if |
| 961 | * we can't find a resolution. |
| 962 | */ |
| 963 | void CVmBifT3::get_named_arg(VMG_ uint argc) |
| 964 | { |
| 965 | /* check arguments */ |
| 966 | check_argc_range(vmg_ argc, 1, 2); |
| 967 | |
| 968 | /* get the name we're looking for */ |
| 969 | const char *name = G_stk->get(0)->get_as_string(vmg0_); |
| 970 | if (name == 0) |
| 971 | err_throw(VMERR_STRING_VAL_REQD); |
| 972 | |
| 973 | /* get the length and buffer pointer */ |
| 974 | size_t namelen = vmb_get_len(name); |
| 975 | name += VMB_LEN; |
| 976 | |
| 977 | /* |
| 978 | * Scan the stack for named parameter tables. A named parameter table |
| 979 | * is always in the calling frame at the stack slot just beyond the |
| 980 | * last argument. |
| 981 | */ |
| 982 | for (vm_val_t *fp = G_interpreter->get_frame_ptr() ; fp != 0 ; |
| 983 | fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp)) |
| 984 | { |
| 985 | /* check for a table in this frame */ |
| 986 | vm_val_t *argp; |
| 987 | const uchar *t = CVmRun::get_named_args_from_frame(vmg_ fp, &argp); |
| 988 | if (t != 0) |
| 989 | { |
| 990 | /* get the number of table entries */ |
| 991 | int n = osrp2(t); |
| 992 | t += 2; |
| 993 | |
| 994 | /* scan the table for the name */ |
| 995 | for (int i = 0 ; n >= 0 ; --n, i += 2, ++argp) |
| 996 | { |
| 997 | /* get this element's offset, and figure its length */ |
| 998 | uint eofs = osrp2(t + i); |
| 999 | uint elen = osrp2(t + i + 2) - eofs; |
| 1000 | |
| 1001 | /* check for a match */ |
| 1002 | if (elen == namelen && memcmp(name, t + eofs, elen) == 0) |
| 1003 | { |
| 1004 | /* found it - return the value */ |
| 1005 | retval(vmg_ argp); |
| 1006 | |
| 1007 | /* discard arguments and return */ |
| 1008 | G_stk->discard(argc); |
| 1009 | return; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * The argument is undefined. If a default value was supplied, simply |
| 1017 | * return the default value. Otherwise throw an error. |
| 1018 | */ |
| 1019 | if (argc >= 2) |
| 1020 | { |
| 1021 | /* a default value was supplied - simply return it */ |
| 1022 | retval(vmg_ G_stk->get(1)); |
| 1023 | |
| 1024 | /* discard arguments */ |
| 1025 | G_stk->discard(argc); |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | /* no default value - throw an error */ |
| 1030 | err_throw_a(VMERR_MISSING_NAMED_ARG, 1, ERR_TYPE_TEXTCHAR_LEN, |
| 1031 | name, namelen); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Retrieve a list of the named argument names. |
| 1037 | */ |
| 1038 | void CVmBifT3::get_named_arg_list(VMG_ uint argc) |
| 1039 | { |
| 1040 | /* check arguments */ |
| 1041 | check_argc(vmg_ argc, 0); |
| 1042 | |
| 1043 | /* create the result list; we'll expand as necessary later */ |
| 1044 | G_stk->push()->set_obj(CVmObjList::create(vmg_ FALSE, 10)); |
| 1045 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ G_stk->get(0)->val.obj); |
| 1046 | |
| 1047 | /* clear it out, since we're building it incrementally */ |
| 1048 | lst->cons_clear(); |
| 1049 | |
| 1050 | /* we haven't added any elements yet */ |
| 1051 | int idx = 0; |
| 1052 | |
| 1053 | /* scan the stack and populate the name list from the tables we find */ |
| 1054 | for (vm_val_t *fp = G_interpreter->get_frame_ptr() ; fp != 0 ; |
| 1055 | fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp)) |
| 1056 | { |
| 1057 | /* look for a named argument table in this frame */ |
| 1058 | vm_val_t *argp; |
| 1059 | const uchar *t = CVmRun::get_named_args_from_frame(vmg_ fp, &argp); |
| 1060 | if (t != 0) |
| 1061 | { |
| 1062 | /* get the number of table entries */ |
| 1063 | int n = osrp2(t); |
| 1064 | t += 2; |
| 1065 | |
| 1066 | /* |
| 1067 | * Build the list. The compiler generates the list in |
| 1068 | * right-to-left order (the order of pushing the arguments). |
| 1069 | * For readability, reverse this: generate the list left to |
| 1070 | * right, so that it appears in the original source code order. |
| 1071 | */ |
| 1072 | argp += n - 1; |
| 1073 | for (int i = (n-1)*2 ; i >= 0 ; i -= 2, --argp) |
| 1074 | { |
| 1075 | /* get this string's offset and figure its length */ |
| 1076 | uint ofs = osrp2(t + i); |
| 1077 | uint len = osrp2(t + i + 2) - ofs; |
| 1078 | |
| 1079 | /* create a string from the name */ |
| 1080 | vm_val_t str; |
| 1081 | str.set_obj(CVmObjString::create( |
| 1082 | vmg_ FALSE, (const char *)t + ofs, len)); |
| 1083 | |
| 1084 | /* add it to the list */ |
| 1085 | lst->cons_ensure_space(vmg_ idx, 10); |
| 1086 | lst->cons_set_element(idx, &str); |
| 1087 | ++idx; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /* set the final list length */ |
| 1093 | lst->cons_set_len(idx); |
| 1094 | |
| 1095 | /* keep only the unique elements */ |
| 1096 | lst->cons_uniquify(vmg0_); |
| 1097 | |
| 1098 | /* return the results */ |
| 1099 | retval_pop(vmg0_); |
| 1100 | } |
| 1101 | |
| 1102 | /* ------------------------------------------------------------------------ */ |
| 1103 | /* |
| 1104 | * T3 VM Test function set. This function set contains internal test |
| 1105 | * and debug functions. These functions are not meant for use by |
| 1106 | * "normal" programs - they provide internal access to certain VM state |
| 1107 | * that is not useful or meaningful except for testing and debugging the |
| 1108 | * VM itself. |
| 1109 | */ |
| 1110 | |
| 1111 | /* |
| 1112 | * Get an object's internal ID. Takes an object instance and returns an |
| 1113 | * integer giving the object's VM ID number. This is effectively an |
| 1114 | * address that can be used to refer to the object. Because this value |
| 1115 | * is returned as an integer, it is NOT a reference to the object for |
| 1116 | * the purposes of garbage collection or finalization. |
| 1117 | */ |
| 1118 | void CVmBifT3Test::get_obj_id(VMG_ uint argc) |
| 1119 | { |
| 1120 | vm_val_t val; |
| 1121 | |
| 1122 | /* one argument required */ |
| 1123 | check_argc(vmg_ argc, 1); |
| 1124 | |
| 1125 | /* get the object value */ |
| 1126 | G_interpreter->pop_obj(vmg_ &val); |
| 1127 | |
| 1128 | /* return the object ID as an integer */ |
| 1129 | retval_int(vmg_ (long)val.val.obj); |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Get an object's garbage collection state. Takes an object ID (NOT an |
| 1134 | * object reference -- this is the integer value returned by get_obj_id) |
| 1135 | * and returns a bit mask with the garbage collector state. |
| 1136 | * |
| 1137 | * (retval & 0x000F) gives the free state. 0 is free, 1 is in use. |
| 1138 | * |
| 1139 | * (retval & 0x00F0) gives the reachable state. 0x00 is unreachable, |
| 1140 | * 0x10 is finalizer-reachable, and 0x20 is fully reachable. |
| 1141 | * |
| 1142 | * (retval & 0x0F00) gives the finalizer state. 0x000 is unfinalizable, |
| 1143 | * 0x100 is finalizable, and 0x200 is finalized. |
| 1144 | * |
| 1145 | * (retval & 0xF000) gives the object ID validity. 0 is valid, 0xF000 |
| 1146 | * is invalid. |
| 1147 | */ |
| 1148 | void CVmBifT3Test::get_obj_gc_state(VMG_ uint argc) |
| 1149 | { |
| 1150 | vm_val_t val; |
| 1151 | |
| 1152 | /* one argument required */ |
| 1153 | check_argc(vmg_ argc, 1); |
| 1154 | |
| 1155 | /* pop the string */ |
| 1156 | G_interpreter->pop_int(vmg_ &val); |
| 1157 | |
| 1158 | /* return the internal garbage collector state of the object */ |
| 1159 | retval_int(vmg_ |
| 1160 | (long)G_obj_table->get_obj_internal_state(val.val.intval)); |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Get the Unicode character code of the first character of a string |
| 1165 | */ |
| 1166 | void CVmBifT3Test::get_charcode(VMG_ uint argc) |
| 1167 | { |
| 1168 | const char *str; |
| 1169 | |
| 1170 | /* one argument required */ |
| 1171 | check_argc(vmg_ argc, 1); |
| 1172 | |
| 1173 | /* get the object ID as an integer */ |
| 1174 | str = pop_str_val(vmg0_); |
| 1175 | |
| 1176 | /* |
| 1177 | * if the string is empty, return nil; otherwise, return the Unicode |
| 1178 | * character code of the first character |
| 1179 | */ |
| 1180 | if (vmb_get_len(str) == 0) |
| 1181 | { |
| 1182 | /* empty string - return nil */ |
| 1183 | retval_nil(vmg0_); |
| 1184 | } |
| 1185 | else |
| 1186 | { |
| 1187 | /* |
| 1188 | * get the character code of the first character and return it |
| 1189 | * as an integer |
| 1190 | */ |
| 1191 | retval_int(vmg_ (int)utf8_ptr::s_getch(str + VMB_LEN)); |
| 1192 | } |
| 1193 | } |
| 1194 |