/* * Debug Trace */ void CVmBifT3::debug_trace(VMG_ uint argc) { /* make sure we have at least one argument */ if (argc < 1) err_throw(VMERR_WRONG_NUM_OF_ARGS); /* pop the flags and see what we're being asked to do */ switch(pop_int_val(vmg0_)) { case T3DBG_CHECK: /* check arguments */ check_argc(vmg_ argc, 1); /* we're just being asked if the debugger is present - it's not */ retval_nil(vmg0_); break; case T3DBG_BREAK: /* check arguments */ check_argc(vmg_ argc, 1); /* tell the caller there's no debugger */ retval_nil(vmg0_); break; default: /* anything else just returns nil, to allow for future expansion */ G_stk->discard(argc - 1); retval_nil(vmg0_); break; } }
/* * Get the local host name */ void CVmBifNet::get_hostname(VMG_ uint oargc) { /* check arguments */ check_argc(vmg_ oargc, 0); /* check the network safety levels */ int client_level, server_level; G_host_ifc->get_net_safety(&client_level, &server_level); /* * If the network safety level doesn't allow outside network access for * either client or server, return "localhost". If they don't allow * any network access at all, return nil. * * If there's a host name defined in the web configuration, return that * host name. Otherwise, return the default host name from the OS. */ if (client_level >= VM_NET_SAFETY_MAXIMUM && server_level >= VM_NET_SAFETY_MAXIMUM) { /* no network access is allowed - return nil */ retval_nil(vmg0_); } else if (client_level >= VM_NET_SAFETY_LOCALHOST && server_level >= VM_NET_SAFETY_LOCALHOST) { /* localhost access only - return "localhost" */ retval_str(vmg_ "localhost"); } else { /* * the network safety level allows outside network access, so we * can return the actual host name - get it from the system */ char buf[256]; if (os_get_hostname(buf, sizeof(buf))) { /* got it - return the string */ retval_ui_str(vmg_ buf); } else { /* no host name available - return nil */ retval_nil(vmg0_); } } }
/* * get the runtime symbol table */ void CVmBifT3::get_global_symtab(VMG_ uint argc) { /* check arguments */ check_argc_range(vmg_ argc, 0, 1); /* if there's an argument, it specifies which table to retrieve */ int which = 1; if (argc >= 1) which = pop_int_val(vmg0_); /* return the desired table */ switch (which) { case 1: /* return the loader's symbol table object, if any */ retval_obj(vmg_ G_image_loader->get_reflection_symtab()); break; case 2: /* return the macro table, if any */ retval_obj(vmg_ G_image_loader->get_reflection_macros()); break; case 3: /* other values are allowed but simply return nil */ retval_nil(vmg0_); break; } }
/* * Get the Unicode character code of the first character of a string */ void CVmBifT3Test::get_charcode(VMG_ uint argc) { const char *str; /* one argument required */ check_argc(vmg_ argc, 1); /* get the object ID as an integer */ str = pop_str_val(vmg0_); /* * if the string is empty, return nil; otherwise, return the Unicode * character code of the first character */ if (vmb_get_len(str) == 0) { /* empty string - return nil */ retval_nil(vmg0_); } else { /* * get the character code of the first character and return it * as an integer */ retval_int(vmg_ (int)utf8_ptr::s_getch(str + VMB_LEN)); } }
/* * Get the local host IP address */ void CVmBifNet::get_host_ip(VMG_ uint oargc) { /* check arguments */ check_argc(vmg_ oargc, 0); /* check the network safety levels */ int client_level, server_level; G_host_ifc->get_net_safety(&client_level, &server_level); /* * If the network safety level doesn't allow outside network access for * either client or server, return "localhost". If they don't allow * any network access at all, return nil. */ if (client_level >= VM_NET_SAFETY_MAXIMUM && server_level >= VM_NET_SAFETY_MAXIMUM) { /* no network access is allowed - return nil */ retval_nil(vmg0_); } else if (client_level >= VM_NET_SAFETY_LOCALHOST && server_level >= VM_NET_SAFETY_LOCALHOST) { /* localhost access only - return the standard localhost IP address */ retval_str(vmg_ "127.0.0.1"); } else { /* retrieve the host IP address for the default host */ char buf[256]; if (os_get_local_ip(buf, sizeof(buf), 0)) { /* got it - return the string */ retval_ui_str(vmg_ buf); } else { /* no host name available - return nil */ retval_nil(vmg0_); } } }
/* * Get the launching host address */ void CVmBifNet::get_launch_host_addr(VMG_ uint oargc) { /* check arguments */ check_argc(vmg_ oargc, 0); /* get the launch host name from the network configuration */ const char *host = (G_net_config != 0 ? G_net_config->get("hostname") : 0); /* if there's a host name, return it, otherwise return nil */ if (host != 0) retval_str(vmg_ host); else retval_nil(vmg0_); }
void CVmBifNet::get_net_config(VMG_ uint oargc) { /* check arguments */ check_argc(vmg_ oargc, 1); /* get the variable name */ char name[256]; pop_str_val_buf(vmg_ name, sizeof(name)); /* look up the name */ const char *val = 0; if (G_net_config != 0) val = G_net_config->get(name); /* if we found a value, return it; otherwise return nil */ if (val != 0) retval_str(vmg_ val); else retval_nil(vmg0_); }
/* get the storage server URL */ void CVmBifNet::get_storage_url(VMG_ uint oargc) { /* check arguments */ check_argc(vmg_ oargc, 1); /* set a default nil return in case we can't build the path */ retval_nil(vmg0_); /* get the resource name */ const char *page = G_stk->get(0)->get_as_string(vmg0_); if (page == 0) err_throw(VMERR_STRING_VAL_REQD); /* get the resource name length and buffer pointer */ size_t pagelen = vmb_get_len(page); page += VMB_LEN; /* if there's a network configuration, build the resource path */ const char *host = 0, *rootpath = 0; if (G_net_config != 0) { /* get the storage server host name and root path */ host = G_net_config->get("storage.domain"); rootpath = G_net_config->get("storage.rootpath", "/"); } /* we must have a host name to proceed */ if (host != 0) { /* build the full string */ G_interpreter->push_stringf(vmg_ "http://%s%s%.*s", host, rootpath, (int)pagelen, page); /* pop it into R0 */ G_stk->pop(G_interpreter->get_r0()); } /* discard arguments */ G_stk->discard(); }
void CVmBifT3::get_stack_trace(VMG_ uint argc) { int single_level = 0; int level; vm_val_t *fp; vm_val_t lst_val; CVmObjList *lst; const uchar *entry_addr; ulong method_ofs; vm_val_t stack_info_cls; int want_named_args = FALSE; int want_locals = FALSE; int want_frefs = FALSE; int flags = 0; const vm_rcdesc *rc; /* check arguments */ check_argc_range(vmg_ argc, 0, 2); /* get the imported stack information class */ stack_info_cls.set_obj(G_predef->stack_info_cls); if (stack_info_cls.val.obj == VM_INVALID_OBJ) { /* * there's no stack information class - we can't return any * meaningful information, so just return nil */ retval_nil(vmg0_); return; } /* * look up T3StackInfo.construct() to determine how many arguments it * wants */ { int min_args, opt_args, varargs; if (vm_objp(vmg_ stack_info_cls.val.obj)->get_prop_interface( vmg_ stack_info_cls.val.obj, G_predef->obj_construct, min_args, opt_args, varargs)) { /* check to see how many extra arguments they want */ want_named_args = (min_args + opt_args >= 7 || varargs); want_locals = (min_args + opt_args >= 8 || varargs); want_frefs = (min_args + opt_args >= 9 || varargs); } } /* check to see if we're fetching a single level or the full trace */ if (argc >= 1) { /* * Get the single level, and adjust to a 0 base. If the level is * nil, we're still getting all levels. */ if (G_stk->get(0)->typ == VM_NIL) { /* it's nil - get all levels */ G_stk->discard(); } else { /* get the level number */ single_level = pop_int_val(vmg0_); /* make sure it's in range */ if (single_level <= 0) err_throw(VMERR_BAD_VAL_BIF); /* we won't need a return list */ lst_val.set_obj_or_nil(VM_INVALID_OBJ); lst = 0; } } /* get the flags argument, if present */ if (argc >= 2) flags = pop_int_val(vmg0_); /* if we're not doing a single level, we need a list for the result */ if (!single_level) { /* * We're returning a full list, so we need to allocate the list for * the return value. First, count stack levels to see how big a * list we'll need. */ fp = G_interpreter->get_frame_ptr(); entry_addr = G_interpreter->get_entry_ptr(); method_ofs = G_interpreter->get_method_ofs(); for (level = 0 ; fp != 0 ; fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp), ++level) { /* add an extra level for each system call */ if (method_ofs == 0 && entry_addr != 0) ++level; /* get the return address */ entry_addr = G_interpreter->get_enclosing_entry_ptr_from_frame(vmg_ fp); method_ofs = G_interpreter->get_return_ofs_from_frame(vmg_ fp); } /* create the list */ lst_val.set_obj(CVmObjList::create(vmg_ FALSE, level)); lst = (CVmObjList *)vm_objp(vmg_ lst_val.val.obj); /* * we create other objects while building this list, so the gc * could run - clear the list to ensure it contains valid data */ lst->cons_clear(); /* protect the list from garbage collection while we work */ G_stk->push(&lst_val); /* flag that we're doing the whole stack */ single_level = -1; } else { /* adjust the level to a 0-based index */ single_level -= 1; } /* set up at the current function */ fp = G_interpreter->get_frame_ptr(); entry_addr = G_interpreter->get_entry_ptr(); method_ofs = G_interpreter->get_method_ofs(); rc = 0; /* traverse the frames */ for (level = 0 ; fp != 0 ; ++level) { int fr_argc; int i; vm_obj_id_t def_obj; vm_val_t info_self; vm_val_t info_func; vm_val_t info_obj; vm_val_t info_prop; vm_val_t info_args; vm_val_t info_locals; vm_val_t info_srcloc; vm_val_t info_frameref; CVmObjList *arglst; vm_val_t ele; CVmFuncPtr func_ptr; int gc_cnt = 0; int info_argc = 0; /* if we're looking for a single level, and this isn't it, skip it */ if (single_level >= 0 && level != single_level) goto done_with_level; /* * start with the information values to nil - we'll set the * appropriate ones when we find out what we have */ info_func.set_nil(); info_obj.set_nil(); info_prop.set_nil(); info_self.set_nil(); info_locals.set_nil(); info_frameref.set_nil(); /* get the number of arguments to the function in this frame */ fr_argc = G_interpreter->get_argc_from_frame(vmg_ fp); /* set up a function pointer for the method's entry address */ func_ptr.set(entry_addr); /* get the current frame's defining object */ def_obj = G_interpreter->get_defining_obj_from_frame(vmg_ fp); /* check for special method offsets */ switch (method_ofs) { case VMRUN_RET_OP: /* the real return address is one past the last argument */ method_ofs = G_interpreter->get_param_from_frame(vmg_ fp, argc) ->val.intval; break; case VMRUN_RET_OP_ASILCL: /* the real return address is two past the last argument */ method_ofs = G_interpreter->get_param_from_frame(vmg_ fp, argc+1) ->val.intval; break; } /* determine whether it's an object.prop or a function call */ if (method_ofs == 0) { /* * A zero method offset indicates a recursive VM invocation * from a native function. Presume we have no information on * the caller. */ info_self.set_nil(); fr_argc = 0; /* check for a native caller context */ if (rc != 0) { /* check which kind of native caller we have */ if (rc->bifptr.typ != VM_NIL) { /* we have a built-in function at this level */ info_func = rc->bifptr; } else if (rc->self.typ != VM_NIL) { /* it's an intrinsic class method - get the 'self' */ info_obj = info_self = rc->self; /* get the metaclass */ CVmMetaclass *mc; switch (info_obj.typ) { case VM_OBJ: /* get the metaclass from the object */ mc = vm_objp(vmg_ info_obj.val.obj) ->get_metaclass_reg(); break; case VM_LIST: /* list constant - use the List metaclass */ mc = CVmObjList::metaclass_reg_; break; case VM_SSTRING: /* string constant - use the String metaclass */ mc = CVmObjString::metaclass_reg_; break; default: /* other types don't have metaclasses */ mc = 0; break; } /* get the registration table entry */ vm_meta_entry_t *me = mc == 0 ? 0 : G_meta_table->get_entry_from_reg(mc->get_reg_idx()); /* get the metaclass and property from the entry */ if (me != 0) { /* set 'obj' to the IntrinsicClass object */ info_obj.set_obj(me->class_obj_); /* get the property ID */ info_prop.set_propid(me->xlat_func(rc->method_idx)); } } } } else if (def_obj == VM_INVALID_OBJ) { /* there's no defining object, so this is a function call */ func_ptr.get_fnptr(vmg_ &info_func); } else { /* it's an object.prop invocation */ info_obj.set_obj(def_obj); // $$$ walk up to base modified obj? info_prop.set_propid( G_interpreter->get_target_prop_from_frame(vmg_ fp)); /* get the 'self' in this frame */ info_self.set_obj(G_interpreter->get_self_from_frame(vmg_ fp)); } /* * build the argument list and source location, except for system * routines */ if (method_ofs != 0 || rc != 0) { /* allocate a list object to store the argument list */ int ac = (rc != 0 ? rc->argc : fr_argc); info_args.set_obj(CVmObjList::create(vmg_ FALSE, ac)); arglst = (CVmObjList *)vm_objp(vmg_ info_args.val.obj); /* push the argument list for gc protection */ G_stk->push(&info_args); ++gc_cnt; /* build the argument list */ for (i = 0 ; i < ac ; ++i) { /* add this element to the argument list */ const vm_val_t *v = (rc != 0 ? rc->argp - i : G_interpreter->get_param_from_frame(vmg_ fp, i)); arglst->cons_set_element(i, v); } /* get the source location */ get_source_info(vmg_ entry_addr, method_ofs, &info_srcloc); /* * if they want locals, and this isn't a recursive native * caller, retrieve them */ if (rc == 0 && (((flags & T3_GST_LOCALS) != 0 && want_locals) || ((flags & T3_GST_FREFS) != 0 && want_frefs))) { /* get the locals */ get_stack_locals(vmg_ fp, entry_addr, method_ofs, (flags & T3_GST_LOCALS) != 0 && want_locals ? &info_locals : 0, (flags & T3_GST_FREFS) != 0 && want_frefs ? &info_frameref : 0); /* * that leaves the LookupTable and StackFrameDesc on the * stack, so note that we need to discard the stack level * when we're done with it */ if (info_locals.typ == VM_OBJ) ++gc_cnt; if (info_frameref.typ == VM_OBJ) ++gc_cnt; } } else { /* * it's a system routine - no argument information is * available, so return nil rather than an empty list to to * indicate the absence */ info_args.set_nil(); /* there's obviously no source location for system code */ info_srcloc.set_nil(); } /* * We have all of the information on this level now, so create the * information object for the level. This is an object of the * exported stack-info class, which is a TadsObject type. */ /* start with the original complement of arguments */ info_argc = 7; /* * if we have a modern T3StackInfo object, push the locals, * named argument elements, and frame reference object */ if (want_frefs) { G_stk->push(&info_frameref); ++info_argc; } if (want_named_args) { /* * the constructor has a slot for named arguments - push either * a table or nil, depending... */ vm_val_t *argp; const uchar *t = 0; /* if the flags request locals, retrieve the named arguments */ if ((flags & T3_GST_LOCALS) != 0) t = CVmRun::get_named_args_from_frame(vmg_ fp, &argp); /* * if we do in fact have named arguments, build a lookup table * copy and push it; otherwise just push nil */ if (t != 0) { /* get the number of table entries */ int n = osrp2(t); t += 2; /* create a lookup table for the arguments */ G_stk->push()->set_obj(CVmObjLookupTable::create( vmg_ FALSE, n <= 8 ? 8 : n <= 32 ? 32 : 64, n)); CVmObjLookupTable *lt = (CVmObjLookupTable *)vm_objp( vmg_ G_stk->get(0)->val.obj); /* * Populate the lookup table with the named arguments. The * compiler builds the table in the order pushed, which is * right to left. Lookup tables preserve the order in * which elements are added, and reflect this order in key * lists, so to that extent the order of building the * lookup table matters. For readability of the generated * list, in case it's presented to the user, build the * table in left-to-right order, which is the reverse of * the table order in the bytecode table. */ argp += n - 1; for (int i = (n-1)*2 ; i >= 0 ; i -= 2, --argp) { /* get the name pointer and length from the index */ uint ofs = osrp2(t + i), nxtofs = osrp2(t + i + 2); const char *name = (const char *)t + ofs; size_t len = nxtofs - ofs; /* create a string from the name */ vm_val_t str; str.set_obj(CVmObjString::create(vmg_ FALSE, name, len)); /* add it to the table */ lt->add_entry(vmg_ &str, argp); } } else { /* there are no named arguments - push nil */ G_stk->push()->set_nil(); } /* count the argument */ ++info_argc; } if (want_locals) { G_stk->push(&info_locals); ++info_argc; } /* push the rest of the arguments */ G_stk->push(&info_srcloc); G_stk->push(&info_args); G_stk->push(&info_self); G_stk->push(&info_prop); G_stk->push(&info_obj); G_stk->push(&info_func); G_stk->push(&stack_info_cls); ele.set_obj(CVmObjTads::create_from_stack(vmg_ 0, info_argc)); /* discard the gc protection items */ G_stk->discard(gc_cnt); /* * if we're fetching a single level, this is it - return the new * stack info object and we're done */ if (single_level >= 0) { /* return the single level object */ retval_obj(vmg_ ele.val.obj); /* we're done */ return; } /* add the new element to our list */ lst->cons_set_element(level, &ele); done_with_level: /* * If this is a system call level, and we're not in debug mode, * this recursive frame contains the entry address for the caller, * but not the calling byte-code address. Stay on the current * level in this case. */ if (method_ofs == 0 && entry_addr != 0) { /* * This is a recursive caller, and we have a valid entry * address for the prior frame. Stay in the current frame, and * retrieve the actual return address from the calling frame. */ if (rc != 0) { /* get the actual return address from the recursive context */ method_ofs = rc->return_addr - entry_addr; /* * we're now in the bytecode part of the frame, so forget * the recursive context */ rc = 0; } else { /* no recursive context - use a fake return address */ method_ofs = G_interpreter->get_funchdr_size(); } } else { /* move up to the enclosing frame */ entry_addr = G_interpreter->get_enclosing_entry_ptr_from_frame(vmg_ fp); method_ofs = G_interpreter->get_return_ofs_from_frame(vmg_ fp); rc = G_interpreter->get_rcdesc_from_frame(vmg_ fp); fp = G_interpreter->get_enclosing_frame_ptr(vmg_ fp); } } /* return the list */ retval_obj(vmg_ lst_val.val.obj); /* discard our gc protection */ G_stk->discard(); }
/* * Connect to the Web UI */ void CVmBifNet::connect_ui(VMG_ uint oargc) { /* check arguments */ check_argc(vmg_ oargc, 2); /* get the server object */ vm_val_t *srv = G_stk->get(0); if (srv->typ != VM_OBJ || !CVmObjHTTPServer::is_httpsrv_obj(vmg_ srv->val.obj)) err_throw(VMERR_BAD_TYPE_BIF); /* get the object pointer properly cast */ CVmObjHTTPServer *srvp = (CVmObjHTTPServer *)vm_objp(vmg_ srv->val.obj); /* get the URL path */ const char *path = G_stk->get(1)->get_as_string(vmg0_); if (path == 0) err_throw(VMERR_BAD_TYPE_BIF); /* make a null-terminated copy of the path */ char *pathb = lib_copy_str(path + VMB_LEN, vmb_get_len(path)); /* get the server network address information */ char *errmsg = 0; char *addr = 0, *ip = 0; int port; int ok = srvp->get_listener_addr(addr, ip, port); /* * If we don't have a network configuration yet, create one. This * notifies other subsystems that we have an active web UI; for * example, the presence of a network UI disables the regular console * UI, since all UI actions have to go through the web UI once it's * established. * * The interpreter startup creates a network configuration if it * receives command-line information telling it that the game was * launched by a Web server in response to an incoming client request. * When the user launches the game in stand-alone mode directly from * the operating system shell, there's no Web server launch * information, so the startup code doesn't create a net config object. * So, if we get here and find we don't have this object, it means that * we're running in local standalone mode. */ if (G_net_config == 0) G_net_config = new TadsNetConfig(); /* connect */ if (ok) { /* connect */ ok = osnet_connect_webui(vmg_ addr, port, pathb, &errmsg); } else { /* couldn't get the server address */ errmsg = lib_copy_str( "No address information available for HTTPServer"); } /* free strings */ lib_free_str(pathb); lib_free_str(addr); lib_free_str(ip); /* if there's an error, throw it */ if (!ok) { G_interpreter->push_string(vmg_ errmsg); lib_free_str(errmsg); G_interpreter->throw_new_class(vmg_ G_predef->net_exception, 1, "Error connecting to Web UI"); } /* no return value */ retval_nil(vmg0_); }