// ECMA262 15.3.4.2 static ejsval _ejs_Function_prototype_toString (ejsval env, ejsval _this, uint32_t argc, ejsval *args) { char terrible_fixed_buffer[256]; if (!EJSVAL_IS_FUNCTION(_this)) _ejs_throw_nativeerror_utf8 (EJS_TYPE_ERROR, "Function.prototype.toString is not generic."); ejsval func_name = _ejs_object_getprop (_this, _ejs_atom_name); char *utf8_funcname = _ejs_string_to_utf8(EJSVAL_TO_STRING(func_name)); snprintf(terrible_fixed_buffer, sizeof (terrible_fixed_buffer), "function %s() {}", utf8_funcname); free (utf8_funcname); return _ejs_string_new_utf8(terrible_fixed_buffer); }
ejsval _ejs_module_get (ejsval arg) { char* arg_utf8 = _ejs_string_to_utf8(EJSVAL_TO_STRING(arg)); ejsval module EJSVAL_ALIGNMENT; if (require_builtin_module (arg_utf8, &module)) { free (arg_utf8); return module; } if (require_external_module (arg_utf8, &module)) { free (arg_utf8); return module; } if (require_user_module (arg_utf8, &module)) { free (arg_utf8); return module; } _ejs_log ("require('%s') failed: module not included in build.\n", arg_utf8); free(arg_utf8); return _ejs_null; }
static ejsval Encode (ejsval string, ejsval unescaped) { EJSPrimString *stringObj = EJSVAL_TO_STRING(string); jschar *unescapedStr = EJSVAL_TO_FLAT_STRING(unescaped); /* 1. Let strLen be the number of code units in string. */ int32_t strLen = stringObj->length; /* 2. Let R be the empty String. */ ejsval R = _ejs_atom_empty; /* 3. Let k be 0. */ int32_t k = 0; /* 4. Repeat. */ for (;;) { /* a. If k equals strLen, return R. */ if (k == strLen) return R; /* b. Let C be the code unit at index k within string. */ jschar C = _ejs_string_ucs2_at (stringObj, k); jschar C_arr [2] = { C, 0 }; /* c. If C is in unescapedSet, then */ jschar *p = ucs2_strstr (unescapedStr, C_arr); if (p) { /* i. Let S be a String containing only the code unit C. */ ejsval S = _ejs_string_new_substring (string, k, 1); /* ii. Let R be a new String value computed by concatenating the previous value of R and S. */ R = _ejs_string_concat (R, S); } /* d. Else C is not in unescapedSet, */ else { /* i. If the code unit value of C is not less than 0xDC00 and not greater than 0xDFFF, throw a URIError exception. */ if (C >= 0xDC00 && C <= 0xDFFF) _ejs_throw_nativeerror_utf8 (EJS_URI_ERROR, "URI malformed"); /* ii. If the code unit value of C is less than 0xD800 or greater than 0xDBFF, then Let V be the code unit value of C. */ jschar V; if (C < 0xD800 || C > 0xDBFF) V = C; /* iii. Else, */ else { /* 1. Increase k by 1. */ k++; /* 2. If k equals strLen, throw a URIError exception. */ if (k == strLen) _ejs_throw_nativeerror_utf8 (EJS_URI_ERROR, "URI malformed"); /* 3. Let kChar be the code unit value of the code unit at index k within string. */ jschar kChar = _ejs_string_ucs2_at (stringObj, k); /* 4. If kChar is less than 0xDC00 or greater than 0xDFFF, throw a URIError exception. */ if (kChar < 0xDC00 || kChar > 0xDFFF) _ejs_throw_nativeerror_utf8 (EJS_URI_ERROR, "URI malformed"); /* 5. Let V be (((the code unit value of C) – 0xD800) × 0x400 + (kChar – 0xDC00) + 0x10000). */ V = (C - 0xD800) * 0x400 + (kChar - 0xDC00) + 0x10000; } /* iv. Let Octets be the array of octets resulting by applying the UTF-8 transformation to V, and let L be the array size. */ char octets[4]; int32_t L = ucs2_to_utf8_char (V, octets); /* v. Let j be 0. */ int32_t j = 0; /* vi. Repeat, while j < L. */ while (j < L) { /* 1. Let jOctet be the value at index j within Octets. */ char jOctet = octets [j]; /* 2. Let S be a String containing three code units “%XY” where XY are two uppercase hexadecimal * digits encoding the value of jOctet. */ char buff[4]; sprintf(buff, "%%%X", jOctet); ejsval S = _ejs_string_new_utf8 (buff); /* 3. Let R be a new String value computed by concatenating the previous value of R and S. */ R = _ejs_string_concat (R, S); /* 4. Increase j by 1. */ j++; } } /* e. Increase k by 1. */ k++; } }