/* * Copyright (C) 2010-2015 Marvell International Ltd. * Copyright (C) 2002-2010 Kinoma, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ void fxBuildJSON(txMachine* the) { txSlot* xs; mxPush(mxGlobal); mxPush(mxObjectPrototype); fxNewObjectInstance(the); fxNewHostFunction(the, fx_JSON_parse, 2); fxQueueID(the, fxID(the, "parse"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_JSON_stringify, 3); fxQueueID(the, fxID(the, "stringify"), XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "JSON"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; xs = fxGetProperty(the, mxGlobal.value.reference, fxID(the, "xs")); mxPush(*xs); mxPush(mxObjectPrototype); fxNewObjectInstance(the); fxNewHostFunction(the, fx_JSON_parse, 1); fxQueueID(the, fxID(the, "parse"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); fxNewHostFunction(the, fx_JSON_stringify, 1); fxQueueID(the, fxID(the, "serialize"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); fxQueueID(the, fxID(the, "json"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; }
txSlot* fxNextHostAccessorProperty(txMachine* the, txSlot* property, txCallback get, txCallback set, txID id, txFlag flag) { txSlot *getter = NULL, *setter = NULL, *home = the->stack, *slot; if (get) { getter = fxNewHostFunction(the, get, 0, id); slot = mxFunctionInstanceHome(getter); slot->kind = home->kind; slot->value = home->value; } if (set) { setter = fxNewHostFunction(the, set, 1, id); slot = mxFunctionInstanceHome(setter); slot->kind = home->kind; slot->value = home->value; } property = property->next = fxNewSlot(the); property->flag = flag; property->ID = id; property->kind = XS_ACCESSOR_KIND; property->value.accessor.getter = getter; property->value.accessor.setter = setter; if (set) the->stack++; if (get) the->stack++; return property; }
txSlot* fxNewHostAccessorGlobal(txMachine* the, txCallback get, txCallback set, txID id, txFlag flag) { txSlot *getter = NULL, *setter = NULL, *property; if (get) getter = fxNewHostFunction(the, get, 0, id); if (set) getter = fxNewHostFunction(the, set, 1, id); property = fxSetGlobalProperty(the, mxGlobal.value.reference, id, C_NULL); property->flag = flag; property->kind = XS_ACCESSOR_KIND; property->value.accessor.getter = getter; property->value.accessor.setter = setter; if (set) the->stack++; if (get) the->stack++; return property; }
txSlot* fxNewHostFunctionGlobal(txMachine* the, txCallback call, txInteger length, txID id, txFlag flag) { txSlot *function, *property; function = fxNewHostFunction(the, call, length, id); property = fxSetGlobalProperty(the, mxGlobal.value.reference, id, C_NULL); property->flag = flag; property->kind = the->stack->kind; property->value = the->stack->value; return function; }
void fxBuildFunction(txMachine* the) { mxPush(mxGlobal); mxPush(mxObjectPrototype); fxNewFunctionInstance(the); fxNewHostFunction(the, fx_Function_get_length, 0); fxQueueID(the, the->lengthID, XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG | XS_GETTER_FLAG); fxNewHostFunction(the, fx_Function_get_prototype, 0); fxQueueID(the, the->prototypeID, XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_GETTER_FLAG); fxNewHostFunction(the, fx_Function_set_prototype, 1); fxQueueID(the, the->prototypeID, XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_SETTER_FLAG); fxNewHostFunction(the, fx_Function_apply, 2); fxQueueID(the, fxID(the, "apply"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Function_bind, 1); fxQueueID(the, fxID(the, "bind"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Function_call, 1); fxQueueID(the, fxID(the, "call"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxFunctionPrototype = *the->stack; fxNewHostConstructor(the, fx_Function, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxFunctionPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "Function"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; }
txSlot* fxNextHostFunctionProperty(txMachine* the, txSlot* property, txCallback call, txInteger length, txID id, txFlag flag) { txSlot *function, *home = the->stack, *slot; function = fxNewHostFunction(the, call, length, id); slot = mxFunctionInstanceHome(function); slot->kind = home->kind; slot->value = home->value; property = property->next = fxNewSlot(the); property->flag = flag; property->ID = id; property->kind = the->stack->kind; property->value = the->stack->value; the->stack++; return property; }
void fxBuildBoolean(txMachine* the) { mxPush(mxGlobal); mxPush(mxObjectPrototype); fxNewBooleanInstance(the); fxNewHostFunction(the, fx_Boolean_toString, 0); fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Boolean_toString, 0); fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Boolean_valueOf, 0); fxQueueID(the, fxID(the, "valueOf"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxBooleanPrototype = *the->stack; fxNewHostConstructor(the, fx_Boolean, 1); //fxAliasInstance(the, the->stack); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxBooleanPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "Boolean"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; }
txSlot* fxNewHostConstructor(txMachine* the, txCallback theCallback, txInteger theLength, txInteger name) { txSlot* aStack; txSlot* instance; txSlot* property; fxToInstance(the, the->stack); aStack = the->stack; instance = fxNewHostFunction(the, theCallback, theLength, name); property = fxLastProperty(the, instance); fxNextSlotProperty(the, property, aStack, mxID(_prototype), XS_GET_ONLY); property = fxSetProperty(the, fxGetInstance(the, aStack), mxID(_constructor), C_NULL); property->flag = XS_DONT_ENUM_FLAG; property->kind = the->stack->kind; property->value = the->stack->value; *aStack = *the->stack; the->stack++; return instance; }
void fxBuildHosts(txMachine* the, txInteger c, txHostFunctionBuilder* builder) { mxPushInteger(c); mxPushInteger(1); mxPush(mxArrayPrototype); fxNewArrayInstance(the); fxArrayCacheBegin(the, the->stack); while (c) { if (builder->length >= 0) fxNewHostFunction(the, builder->callback, builder->length, (builder->id >= 0) ? ((txID*)(the->code))[builder->id] : XS_NO_ID); else fxNewHostObject(the, (txDestructor)builder->callback); fxArrayCacheItem(the, the->stack + 1, the->stack); the->stack++; c--; builder++; } fxArrayCacheEnd(the, the->stack); }
void fxBuildNumber(txMachine* the) { mxPush(mxGlobal); mxPush(mxObjectPrototype); fxNewNumberInstance(the); fxNewHostFunction(the, fx_Number_toExponential, 1); fxQueueID(the, fxID(the, "toExponential"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Number_toFixed, 1); fxQueueID(the, fxID(the, "toFixed"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Number_toString, 0); fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Number_toPrecision, 1); fxQueueID(the, fxID(the, "toPrecision"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Number_toString, 1); fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Number_valueOf, 0); fxQueueID(the, fxID(the, "valueOf"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxNumberPrototype = *the->stack; fxNewHostConstructor(the, fx_Number, 1); mxPushNumber(C_DBL_MAX); fxQueueID(the, fxID(the, "MAX_VALUE"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPushNumber(C_DBL_MIN); fxQueueID(the, fxID(the, "MIN_VALUE"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPushNumber(C_NAN); fxQueueID(the, fxID(the, "NaN"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPushNumber(-C_INFINITY); fxQueueID(the, fxID(the, "NEGATIVE_INFINITY"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPushNumber(C_INFINITY); fxQueueID(the, fxID(the, "POSITIVE_INFINITY"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); //fxAliasInstance(the, the->stack); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxNumberPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "Number"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; }
void fxBuildError(txMachine* the) { mxPush(mxGlobal); mxPush(mxObjectPrototype); fxNewObjectInstance(the); mxPushStringC("Error"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Error_toString, 0); fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_Error_toString, 0); fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_Error, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "Error"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPush(mxErrorPrototype); fxNewObjectInstance(the); mxPushStringC("EvalError"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxEvalErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_EvalError, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxEvalErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "EvalError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPush(mxErrorPrototype); fxNewObjectInstance(the); mxPushStringC("RangeError"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxRangeErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_RangeError, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxRangeErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "RangeError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPush(mxErrorPrototype); fxNewObjectInstance(the); mxPushStringC("ReferenceError"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxReferenceErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_ReferenceError, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxReferenceErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "ReferenceError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPush(mxErrorPrototype); fxNewObjectInstance(the); mxPushStringC("SyntaxError"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxSyntaxErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_SyntaxError, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxSyntaxErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "SyntaxError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPush(mxErrorPrototype); fxNewObjectInstance(the); mxPushStringC("TypeError"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxTypeErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_TypeError, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxTypeErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "TypeError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxPush(mxErrorPrototype); fxNewObjectInstance(the); mxPushStringC("URIError"); fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG); mxPush(mxEmptyString); fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxURIErrorPrototype = *the->stack; fxNewHostConstructor(the, fx_URIError, 1); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxURIErrorPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "URIError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; }
void fxBuildString(txMachine* the) { mxPush(mxGlobal); mxPush(mxObjectPrototype); fxNewStringInstance(the); fxNewHostFunction(the, fx_String_charAt, 1); fxQueueID(the, fxID(the, "charAt"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_charCodeAt, 1); fxQueueID(the, fxID(the, "charCodeAt"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_compare, 1); fxQueueID(the, fxID(the, "compare"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_concat, 1); fxQueueID(the, fxID(the, "concat"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_indexOf, 1); fxQueueID(the, fxID(the, "indexOf"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_lastIndexOf, 1); fxQueueID(the, fxID(the, "lastIndexOf"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_compare, 1); fxQueueID(the, fxID(the, "localeCompare"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_match, 1); fxQueueID(the, fxID(the, "match"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_replace, 2); fxQueueID(the, fxID(the, "replace"), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); fxNewHostFunction(the, fx_String_search, 1); fxQueueID(the, fxID(the, "search"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_slice, 2); fxQueueID(the, fxID(the, "slice"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_split, 2); fxQueueID(the, fxID(the, "split"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_substring, 2); fxQueueID(the, fxID(the, "substring"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_toLowerCase, 0); fxQueueID(the, fxID(the, "toLocaleLowerCase"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_valueOf, 0); fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_toUpperCase, 0); fxQueueID(the, fxID(the, "toLocaleUpperCase"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_toLowerCase, 0); fxQueueID(the, fxID(the, "toLowerCase"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_valueOf, 0); fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_toUpperCase, 0); fxQueueID(the, fxID(the, "toUpperCase"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_trim, 0); fxQueueID(the, fxID(the, "trim"), XS_DONT_ENUM_FLAG); fxNewHostFunction(the, fx_String_valueOf, 0); fxQueueID(the, fxID(the, "valueOf"), XS_DONT_ENUM_FLAG); fxAliasInstance(the, the->stack); mxStringPrototype = *the->stack; fxNewHostConstructor(the, fx_String, 1); fxNewHostFunction(the, fx_String_fromCharCode, 1); fxQueueID(the, fxID(the, "fromCharCode"), XS_DONT_ENUM_FLAG); the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG; *(--the->stack) = mxStringPrototype; fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG); fxQueueID(the, fxID(the, "String"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); the->stack++; }