ejsval _ejs_invoke_closure (ejsval closure, ejsval _this, uint32_t argc, ejsval* args) { if (!EJSVAL_IS_FUNCTION(closure)) { #if DEBUG_LAST_LOOKUP extern jschar* last_lookup; if (last_lookup) { char *last_utf8 = ucs2_to_utf8(last_lookup); _ejs_log ("last property lookup was for: %s\n", last_utf8); free (last_utf8); } #endif _ejs_throw_nativeerror_utf8 (EJS_TYPE_ERROR, "object not a function"); } #if 0 if (!EJSVAL_IS_NULL_OR_UNDEFINED(_this) && EJSVAL_IS_PRIMITIVE(_this)) { _this = ToObject(_this); } #endif EJSFunction *fun = (EJSFunction*)EJSVAL_TO_OBJECT(closure); return fun->func (fun->env, _this, argc, args); }
// returns an EJSPrimString*. // maybe we could change it to return a char* to match ToDouble? that way string concat wouldn't create // temporary strings for non-PrimString objects only to throw them away after concatenation? ejsval ToString(ejsval exp) { if (EJSVAL_IS_MAGIC_IMPL(exp)) { // holes in dense arrays end up here return _ejs_atom_empty; } else if (EJSVAL_IS_NULL(exp)) return _ejs_atom_null; else if (EJSVAL_IS_UNDEFINED(exp)) return _ejs_atom_undefined; else if (EJSVAL_IS_BOOLEAN(exp)) return EJSVAL_TO_BOOLEAN(exp) ? _ejs_atom_true : _ejs_atom_false; else if (EJSVAL_IS_NUMBER(exp)) return NumberToString(EJSVAL_TO_NUMBER(exp)); else if (EJSVAL_IS_STRING(exp)) return exp; else if (EJSVAL_IS_OBJECT(exp)) { ejsval toString = _ejs_object_getprop (exp, _ejs_atom_toString); if (!EJSVAL_IS_FUNCTION(toString)) { return _ejs_Object_prototype_toString(_ejs_null, exp, 0, NULL); } // should we be checking if this returns a string? i'd assume so... return _ejs_invoke_closure (toString, exp, 0, NULL); } else EJS_NOT_IMPLEMENTED(); }
// 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 = ucs2_to_utf8(EJSVAL_TO_FLAT_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_invoke_closure (ejsval closure, ejsval _this, uint32_t argc, ejsval* args) { if (!EJSVAL_IS_FUNCTION(closure)) { #if DEBUG_LAST_LOOKUP extern jschar* last_lookup; if (last_lookup) { char *last_utf8 = ucs2_to_utf8(last_lookup); _ejs_log ("last property lookup was for: %s\n", last_utf8); free (last_utf8); } #endif _ejs_throw_nativeerror_utf8 (EJS_TYPE_ERROR, "object not a function"); } if (!EJSVAL_IS_NULL_OR_UNDEFINED(_this) && EJSVAL_IS_PRIMITIVE(_this)) { _this = ToObject(_this); } EJSFunction *fun = (EJSFunction*)EJSVAL_TO_OBJECT(closure); return fun->func (fun->env, _this, argc, args); #if not_anymore if (fun->bound_argc > 0) { ejsval* new_args = (ejsval*)malloc(sizeof(ejsval) * (fun->bound_argc + argc)); memmove (new_args, fun->bound_args, sizeof(ejsval) * fun->bound_argc); memmove (&new_args[fun->bound_argc], args, argc); args = new_args; argc += fun->bound_argc; } DEBUG_FUNCTION_ENTER (closure); ejsval rv = fun->func (fun->env, fun->bound_this, argc, args); DEBUG_FUNCTION_EXIT (closure); if (fun->bound_argc > 0) free (args); return rv; #endif }
EJSBool _ejs_decompose_closure (ejsval closure, EJSClosureFunc* func, ejsval* env, ejsval *_this) { if (!EJSVAL_IS_FUNCTION(closure)) _ejs_throw_nativeerror_utf8 (EJS_TYPE_ERROR, "object not a function"); EJSFunction *fun = (EJSFunction*)EJSVAL_TO_OBJECT(closure); if (fun->bound_argc > 0) return EJS_FALSE; *func = fun->func; *env = fun->env; if (fun->bound) *_this = fun->bound_this; return EJS_TRUE; }
ejsval _ejs_op_typeof (ejsval exp) { if (EJSVAL_IS_NULL(exp)) return _ejs_atom_null; else if (EJSVAL_IS_BOOLEAN(exp)) return _ejs_atom_boolean; else if (EJSVAL_IS_STRING(exp)) return _ejs_atom_string; else if (EJSVAL_IS_SYMBOL(exp)) return _ejs_atom_symbol; else if (EJSVAL_IS_NUMBER(exp)) return _ejs_atom_number; else if (EJSVAL_IS_UNDEFINED(exp)) return _ejs_atom_undefined; else if (EJSVAL_IS_OBJECT(exp)) { if (EJSVAL_IS_FUNCTION(exp)) return _ejs_atom_function; else return _ejs_atom_object; } else EJS_NOT_IMPLEMENTED(); }
ejsval _ejs_op_typeof_is_function(ejsval exp) { return EJSVAL_IS_FUNCTION(exp) ? _ejs_true : _ejs_false; }
ejsval _ejs_regexp_replace(ejsval str, ejsval search_re, ejsval replace) { EJSRegExp* re = (EJSRegExp*)EJSVAL_TO_OBJECT(search_re); pcre16_extra extra; memset (&extra, 0, sizeof(extra)); pcre16* code = (pcre16*)re->compiled_pattern; int capture_count; pcre16_fullinfo (code, NULL, PCRE_INFO_CAPTURECOUNT, &capture_count); int ovec_count = 3 * (1 + capture_count); int* ovec = malloc(sizeof(int) * ovec_count); int cur_off = 0; do { EJSPrimString *flat_str = _ejs_string_flatten (str); jschar *chars_str = flat_str->data.flat; int rv = pcre16_exec(code, &extra, chars_str, flat_str->length, cur_off, PCRE_NO_UTF16_CHECK, ovec, ovec_count); if (rv < 0) break; ejsval replaceval; if (EJSVAL_IS_FUNCTION(replace)) { ejsval substr_match = _ejs_string_new_substring (str, ovec[0], ovec[1] - ovec[0]); ejsval capture = _ejs_string_new_substring (str, ovec[2], ovec[3] - ovec[2]); _ejs_log ("substring match is %s\n", ucs2_to_utf8(_ejs_string_flatten(substr_match)->data.flat)); _ejs_log ("capture is %s\n", ucs2_to_utf8(_ejs_string_flatten(capture)->data.flat)); int argc = 3; ejsval args[3]; args[0] = substr_match; args[1] = capture; args[2] = _ejs_undefined; replaceval = ToString(_ejs_invoke_closure (replace, _ejs_undefined, argc, args)); } else { replaceval = ToString(replace); } if (ovec[0] == 0) { // we matched from the beginning of the string, so nothing from there to prepend str = _ejs_string_concat (replaceval, _ejs_string_new_substring (str, ovec[1], flat_str->length - ovec[1])); } else { str = _ejs_string_concatv (_ejs_string_new_substring (str, 0, ovec[0]), replaceval, _ejs_string_new_substring (str, ovec[1], flat_str->length - ovec[1]), _ejs_null); } cur_off = ovec[1]; // if the RegExp object was created without a 'g' flag, only replace the first match if (!re->global) break; } while (EJS_TRUE); free (ovec); return str; }