void fx_Function_prototype_bound(txMachine* the) { txSlot* boundArguments; txInteger c, i; txSlot* argument; mxPush(*mxFunction); fxGetID(the, mxID(_boundArguments)); boundArguments = fxGetInstance(the, the->stack); the->stack++; c = boundArguments->next->value.array.length; argument = boundArguments->next->value.array.address; for (i = 0; i < c; i++) { mxPushSlot(argument); argument++; } for (i = 0; i < mxArgc; i++) { mxPushSlot(mxArgv(i)); } /* ARGC */ mxPushInteger(c + mxArgc); /* THIS */ mxPushSlot(mxFunction); fxGetID(the, mxID(_boundThis)); /* FUNCTION */ mxPushSlot(mxFunction); fxGetID(the, mxID(_boundFunction)); fxCall(the); mxPullSlot(mxResult); }
void fx_Error_toString(txMachine* the) { txInteger aLength; mxPushSlot(mxThis); fxGetID(the, mxID(_name)); if (the->stack->kind == XS_UNDEFINED_KIND) fxCopyStringC(the, the->stack, "Error"); else fxToString(the, the->stack); mxPushSlot(mxThis); fxGetID(the, mxID(_message)); if (the->stack->kind == XS_UNDEFINED_KIND) fxCopyStringC(the, the->stack, ""); else fxToString(the, the->stack); aLength = c_strlen(the->stack->value.string); if (aLength) { aLength += c_strlen((the->stack + 1)->value.string) + 2; mxResult->value.string = (txString)fxNewChunk(the, aLength + 1); mxResult->kind = XS_STRING_KIND; c_strcpy(mxResult->value.string, (the->stack + 1)->value.string); c_strcat(mxResult->value.string, ": "); c_strcat(mxResult->value.string, the->stack->value.string); the->stack++; the->stack++; } else { the->stack++; mxPullSlot(mxResult); } }
txSlot* fxConstructArrayResult(txMachine* the, txSlot* constructor, txUnsigned length) { mxPushUnsigned(length); mxPushInteger(1); if (constructor) { mxPushSlot(constructor); fxGetID(the, mxID(_Symbol_species)); } else { txSlot* instance = mxThis->value.reference; if (mxIsArray(instance)) { mxPushSlot(mxThis); fxGetID(the, mxID(_constructor)); if (mxIsReference(the->stack) && mxIsFunction(the->stack->value.reference)) fxGetID(the, mxID(_Symbol_species)); else the->stack->kind = XS_UNDEFINED_KIND; } else mxPushUndefined(); } if (the->stack->kind == XS_NULL_KIND) the->stack->kind = XS_UNDEFINED_KIND; if (the->stack->kind == XS_UNDEFINED_KIND) { *the->stack = mxGlobal; fxGetID(the, mxID(_Array)); } fxNew(the); mxPullSlot(mxResult); return mxResult->value.reference->next; }
void fxBuildKeys(txMachine* the) { txArchive* archive = the->archive; if (archive && archive->keys) (*archive->keys)(the); else { #ifdef mxParse txSlot* code = &mxIDs; txID c, i; if (archive) { c = archive->symbolCount; code->value.code = (txByte *)fxNewChunk(the, c * sizeof(txID)); code->kind = XS_CODE_KIND; for (i = 0; i < XS_SYMBOL_ID_COUNT; i++) { txString string = archive->symbols[i]; txID id = the->keyIndex; txSlot* description = fxNewSlot(the); description->flag = XS_DONT_ENUM_FLAG; fxCopyStringC(the, description, string); the->keyArray[id] = description; the->keyIndex++; mxID(i) = 0x8000 | id; } for (; i < c; i++) { txString string = archive->symbols[i]; mxID(i) = fxNewNameX(the, string)->ID; } } else { code->value.code = (txByte *)fxNewChunk(the, XS_ID_COUNT * sizeof(txID)); code->kind = XS_CODE_KIND; for (i = 0; i < XS_SYMBOL_ID_COUNT; i++) { txID id = the->keyIndex; txSlot* description = fxNewSlot(the); description->flag = XS_DONT_ENUM_FLAG; fxCopyStringC(the, description, gxIDStrings[i]); the->keyArray[id] = description; the->keyIndex++; mxID(i) = 0x8000 | id; } for (; i < XS_ID_COUNT; i++) { mxID(i) = fxID(the, gxIDStrings[i]); } } #else fxCheck(the, __FILE__, __LINE__); #endif } }
void fx_Function_prototype_apply(txMachine* the) { txInteger c, i; if ((mxArgc < 2) || (mxArgv(1)->kind == XS_UNDEFINED_KIND) || (mxArgv(1)->kind == XS_NULL_KIND)) c = 0; else { fxToInstance(the, mxArgv(1)); mxPushSlot(mxArgv(1)); fxGetID(the, mxID(_length)); c = fxToInteger(the, the->stack); the->stack++; for (i = 0; i < c; i++) { mxPushSlot(mxArgv(1)); fxGetID(the, (txID)i); } } /* ARGC */ mxPushInteger(c); /* THIS */ if (mxArgc < 1) mxPushUndefined(); else mxPushSlot(mxArgv(0)); /* FUNCTION */ mxPushSlot(mxThis); fxCall(the); mxPullSlot(mxResult); }
GPUtype * mxToGPUtype (const mxArray *prhs, GPUmanager *GPUman) { ///mexPrintf("************** begin mx to gputype ****************\n"); //GPUman->extCachePrint(); mxArray * tmp = (mxArray*)prhs; //mexPrintf("-> %p\n",tmp->reserved2); //mexPrintf("-> %p\n",tmp->data.number_array.pdata); //printMx1(tmp, 1); GPUtype *p = (GPUtype *) GPUman->extCacheGetGPUtypePtr( mxID( prhs)); if (p==NULL) { // have to get it with slot number GPUman->extCacheCacheMiss(); mxArray *lhs[1]; mexCallMATLAB(1, &lhs[0], 1, (mxArray**) &prhs, "struct"); int slot = (int) mxGetScalar(mxGetFieldByNumber(lhs[0], 0, 0)); //mexPrintf("-> slot %d\n",slot); mxDestroyArray(lhs[0]); p = (GPUtype *) GPUman->extCacheGetGPUtypePtrBySlot(slot); if (p==NULL) mexErrMsgTxt(ERROR_MXTOGPUTYPE); } //mexPrintf("************** end mx to gputype ****************\n"); return p; }
void fxThrowMessage(txMachine* the, txString path, txInteger line, txError error, txString format, ...) { char message[128] = ""; txInteger length = 0; va_list arguments; txSlot* slot; fxBufferFrameName(the, message, sizeof(message), the->frame, ": "); length = c_strlen(message); va_start(arguments, format); vsnprintf(message + length, sizeof(message) - length, format, arguments); va_end(arguments); if ((error <= XS_NO_ERROR) || (XS_ERROR_COUNT <= error)) error = XS_UNKNOWN_ERROR; slot = fxNewSlot(the); slot->kind = XS_INSTANCE_KIND; slot->value.instance.garbage = C_NULL; slot->value.instance.prototype = mxErrorPrototypes(error).value.reference; mxException.kind = XS_REFERENCE_KIND; mxException.value.reference = slot; slot = fxNextStringProperty(the, slot, message, mxID(_message), XS_DONT_ENUM_FLAG); #ifdef mxDebug fxDebugThrow(the, path, line, message); #endif fxJump(the); }
void fx_Function_prototype_hasInstance(txMachine* the) { txSlot* instance; txSlot* prototype; mxResult->value.boolean = 0; mxResult->kind = XS_BOOLEAN_KIND; if (mxArgc == 0) return; instance = fxGetInstance(the, mxArgv(0)); if (!instance) return; mxPushSlot(mxThis); fxGetID(the, mxID(_prototype)); prototype = fxGetInstance(the, the->stack); if (!prototype) mxTypeError("prototype is no object"); while (instance) { if (instance == prototype) { mxResult->value.boolean = 1; break; } instance = fxGetParent(the, instance); } the->stack++; }
txSlot* fxNewIteratorInstance(txMachine* the, txSlot* iterable) { txSlot* instance; txSlot* result; txSlot* property; instance = fxNewObjectInstance(the); mxPush(mxObjectPrototype); result = fxNewObjectInstance(the); property = fxNextUndefinedProperty(the, result, mxID(_value), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG); property = fxNextBooleanProperty(the, property, 0, mxID(_done), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG); property = fxNextSlotProperty(the, instance, the->stack, mxID(_result), XS_GET_ONLY); property = fxNextSlotProperty(the, property, iterable, mxID(_iterable), XS_GET_ONLY); property = fxNextIntegerProperty(the, property, 0, mxID(_index), XS_GET_ONLY); the->stack++; return instance; }
void fxBuildBoolean(txMachine* the) { static const txHostFunctionBuilder gx_Boolean_prototype_builders[] = { { fx_Boolean_prototype_toString, 0, _toString }, { fx_Boolean_prototype_valueOf, 0, _valueOf }, { C_NULL, 0, 0 }, }; const txHostFunctionBuilder* builder; txSlot* slot; mxPush(mxObjectPrototype); slot = fxLastProperty(the, fxNewBooleanInstance(the)); for (builder = gx_Boolean_prototype_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); mxBooleanPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_Boolean, 1, mxID(_Boolean), XS_DONT_ENUM_FLAG); the->stack++; }
void fxBufferFunctionName(txMachine* the, txString buffer, txSize size, txSlot* function, txString suffix) { txSlot* slot = fxGetProperty(the, function, mxID(_name)); if (slot && ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND))) { c_strncat(buffer, slot->value.string, size - c_strlen(buffer) - 1); c_strncat(buffer, suffix, size - c_strlen(buffer) - 1); } }
void fxBufferObjectName(txMachine* the, txString buffer, txSize size, txSlot* object, txString suffix) { txSlot* slot = fxGetProperty(the, object, mxID(_Symbol_toStringTag)); if (slot && ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND))) { c_strncat(buffer, slot->value.string, size - c_strlen(buffer) - 1); c_strncat(buffer, suffix, size - c_strlen(buffer) - 1); } }
void fxBuildGlobal(txMachine* the) { static const txHostFunctionBuilder gx_global_builders[] = { { fx_decodeURI, 1, _decodeURI }, { fx_decodeURIComponent, 1, _decodeURIComponent }, { fx_encodeURI, 1, _encodeURI }, { fx_encodeURIComponent, 1, _encodeURIComponent }, { fx_escape, 1, _escape }, { fx_eval, 1, _eval }, { fx_trace, 1, _trace }, { fx_unescape, 1, _unescape }, { C_NULL, 0, 0 }, }; const txHostFunctionBuilder* builder; txSlot* slot; fxNewGlobalInstance(the); mxPull(mxGlobal); fxNewInstance(the); mxPull(mxObjectPrototype); mxPush(mxObjectPrototype); fxNewFunctionInstance(the, XS_NO_ID); mxPull(mxFunctionPrototype); for (builder = gx_global_builders; builder->callback; builder++) { fxNewHostFunctionGlobal(the, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); the->stack++; } slot = fxSetGlobalProperty(the, mxGlobal.value.reference, mxID(_undefined), C_NULL); slot->flag = XS_GET_ONLY; mxPush(mxObjectPrototype); slot = fxNewObjectInstance(the); slot = fxNextHostFunctionProperty(the, slot, fx_Iterator_iterator, 0, mxID(_Symbol_iterator), XS_DONT_ENUM_FLAG); mxPull(mxIteratorPrototype); mxPush(mxIteratorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextHostFunctionProperty(the, slot, fx_Enumerator_next, 0, mxID(_next), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG); fxNewHostConstructor(the, fx_Enumerator, 0, XS_NO_ID); mxPull(mxEnumeratorFunction); }
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 fx_Error_aux(txMachine* the) { txSlot* aProperty; if ((mxArgc > 0) && (mxArgv(0)->kind != XS_UNDEFINED_KIND)) { aProperty = fxSetProperty(the, fxGetInstance(the, mxResult), mxID(_message), C_NULL); aProperty->value.string = fxToString(the, mxArgv(0)); aProperty->kind = mxArgv(0)->kind; } }
void fxBuildJSON(txMachine* the) { static const txHostFunctionBuilder gx_JSON_builders[] = { { fx_JSON_parse, 2, _parse }, { fx_JSON_stringify, 3, _stringify }, { C_NULL, 0, 0 }, }; const txHostFunctionBuilder* builder; txSlot* slot; mxPush(mxObjectPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); for (builder = gx_JSON_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); slot = fxNextStringXProperty(the, slot, "JSON", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); slot = fxSetGlobalProperty(the, mxGlobal.value.reference, mxID(_JSON)); slot->flag = XS_DONT_ENUM_FLAG; slot->kind = the->stack->kind; slot->value = the->stack->value; the->stack++; }
void fx_Date_prototype_toPrimitive(txMachine* the) { if (mxThis->kind == XS_REFERENCE_KIND) { txInteger hint = ((mxArgc > 0) && (c_strcmp(fxToString(the, mxArgv(0)), "number") == 0)) ? XS_NUMBER_HINT : XS_STRING_HINT; if (hint == XS_STRING_HINT) { mxPushInteger(0); mxPushSlot(mxThis); fxCallID(the, mxID(_toString)); if (mxIsReference(the->stack)) { the->stack++; mxPushInteger(0); mxPushSlot(mxThis); fxCallID(the, mxID(_valueOf)); } } else { mxPushInteger(0); mxPushSlot(mxThis); fxCallID(the, mxID(_valueOf)); if (mxIsReference(the->stack)) { the->stack++; mxPushInteger(0); mxPushSlot(mxThis); fxCallID(the, mxID(_toString)); } } if (mxIsReference(the->stack)) { if (hint == XS_STRING_HINT) mxTypeError("Cannot coerce object to string"); else mxTypeError("Cannot coerce object to number"); } mxResult->kind = the->stack->kind; mxResult->value = the->stack->value; the->stack++; } else { mxResult->kind = mxThis->kind; mxResult->value = mxThis->value; } }
void fxBuildFunction(txMachine* the) { static const txHostFunctionBuilder gx_Function_prototype_builders[] = { { fx_Function_prototype_apply, 2, _apply }, { fx_Function_prototype_bind, 1, _bind }, { fx_Function_prototype_call, 1, _call }, { fx_Function_prototype_hasInstance, 1, _Symbol_hasInstance }, { C_NULL, 0, 0 }, }; const txHostFunctionBuilder* builder; txSlot* slot; mxPush(mxFunctionPrototype); slot = fxLastProperty(the, the->stack->value.reference); slot = fxNextHostAccessorProperty(the, slot, fx_Function_prototype_get_length, fx_Function_prototype_set_length, mxID(_length), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); slot = fxNextHostAccessorProperty(the, slot, fx_Function_prototype_get_name, fx_Function_prototype_set_name, mxID(_name), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); slot = fxNextHostAccessorProperty(the, slot, fx_Function_prototype_get_prototype, fx_Function_prototype_set_prototype, mxID(_prototype), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG); for (builder = gx_Function_prototype_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); slot = fxNextStringProperty(the, slot, "Function", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); slot = fxNewHostConstructorGlobal(the, fx_Function, 1, mxID(_Function), XS_GET_ONLY); the->stack++; }
void fx_JSON_parse(txMachine* the) { volatile txJSONParser* aParser = C_NULL; txSlot* slot; mxTry(the) { if (mxArgc < 1) mxSyntaxError("no buffer"); aParser = c_malloc(sizeof(txJSONParser)); if (NULL == aParser) mxUnknownError("out of memory"); c_memset((txJSONParser*)aParser, 0, sizeof(txJSONParser)); if (mxArgc > 1) { slot = mxArgv(1); if (slot->kind == XS_REFERENCE_KIND) { slot = slot->value.reference; if (slot->next && ((slot->next->kind == XS_CODE_KIND) || (slot->next->kind == XS_CODE_X_KIND) || (slot->next->kind == XS_CALLBACK_KIND))) aParser->reviver = slot; } } slot = mxArgv(0); if (slot->kind == XS_REFERENCE_KIND) { slot = slot->value.reference->next; if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_HOST_KIND)) { aParser->data = slot->value.host.data; aParser->offset = 0; mxPushSlot(mxArgv(0)); fxGetID(the, mxID(_length)); aParser->size = fxToInteger(the, the->stack++); } } if (!aParser->data) { fxToString(the, mxArgv(0)); aParser->slot = mxArgv(0); aParser->offset = 0; aParser->size = c_strlen(aParser->slot->value.string); } fxParseJSON(the, (txJSONParser*)aParser); mxPullSlot(mxResult); c_free((txJSONParser*)aParser); // @@ reviver } mxCatch(the) { if (aParser) c_free((txJSONParser*)aParser); fxJump(the); } }
void fxDefaultFunctionPrototype(txMachine* the, txSlot* function, txSlot* prototype) { txSlot* instance; txSlot* property; instance = fxNewSlot(the); instance->kind = XS_INSTANCE_KIND; instance->value.instance.garbage = C_NULL; instance->value.instance.prototype = mxObjectPrototype.value.reference; prototype->kind = XS_REFERENCE_KIND; prototype->value.reference = instance; property = instance->next = fxNewSlot(the); property->flag = XS_DONT_ENUM_FLAG; property->ID = mxID(_constructor); property->kind = XS_REFERENCE_KIND; property->value.reference = function; }
void fxToPrimitive(txMachine* the, txSlot* theSlot, txInteger theHint) { if (theSlot->kind == XS_REFERENCE_KIND) { fxBeginHost(the); if (theHint == XS_NO_HINT) mxPushString(mxDefaultString.value.string); else if (theHint == XS_NUMBER_HINT) mxPushString(mxNumberString.value.string); else mxPushString(mxStringString.value.string); mxPushInteger(1); mxPushSlot(theSlot); fxCallID(the, mxID(_Symbol_toPrimitive)); theSlot->kind = the->stack->kind; theSlot->value = the->stack->value; the->stack++; fxEndHost(the); } }
void fxBufferFrameName(txMachine* the, txString buffer, txSize size, txSlot* frame, txString suffix) { txSlot* target = frame + 2; txSlot* function = frame + 3; txSlot* _this = frame + 4; if (function->kind == XS_REFERENCE_KIND) { function = function->value.reference; if (mxIsFunction(function)) { if (target->kind == XS_UNDEFINED_KIND) { txSlot* home = mxFunctionInstanceHome(function); if (home->kind == XS_REFERENCE_KIND) { home = home->value.reference; if (mxIsFunction(home)) { fxBufferFunctionName(the, buffer, size, home, "."); } else { txSlot* constructor = fxGetOwnProperty(the, home, mxID(_constructor)); if (constructor) { if (constructor->kind == XS_REFERENCE_KIND) { constructor = constructor->value.reference; if (mxIsFunction(constructor)) fxBufferFunctionName(the, buffer, size, constructor, ".prototype."); } } else if (_this->kind == XS_REFERENCE_KIND) { fxBufferObjectName(the, buffer, size, _this->value.reference, "."); } } } } fxBufferFunctionName(the, buffer, size, function, ""); } } else c_strncat(buffer, "(host)", size - c_strlen(buffer) - 1); c_strncat(buffer, suffix, size - c_strlen(buffer) - 1); }
txSlot* fxCreateInstance(txMachine* the, txSlot* slot) { txSlot* prototype; mxPushUndefined(); prototype = the->stack; fxBeginHost(the); mxPushReference(slot); fxGetID(the, mxID(_prototype)); *prototype = *the->stack; fxEndHost(the); /* txSlot* prototype = mxFunctionInstancePrototype(slot); if (prototype->kind == XS_NULL_KIND) { if (prototype->flag & XS_DONT_SET_FLAG) mxTypeError("new.target: no constructor"); if (slot->flag & XS_SHARED_FLAG) mxTypeError("new.target: shared"); fxDefaultFunctionPrototype(the, slot, prototype); } mxPushSlot(prototype); */ fxNewInstanceOf(the); return the->stack->value.reference; }
void fxBuildSymbol(txMachine* the) { static const txHostFunctionBuilder gx_Symbol_prototype_builders[] = { { fx_Symbol_prototype_toString, 0, _toString }, { fx_Symbol_prototype_valueOf, 0, _valueOf }, { C_NULL, 0, 0 }, }; static const txHostFunctionBuilder gx_Symbol_builders[] = { { fx_Symbol_for, 1, _for }, { fx_Symbol_keyFor, 1, _keyFor }, { C_NULL, 0, 0 }, }; const txHostFunctionBuilder* builder; txSlot* slot; mxPush(mxObjectPrototype); slot = fxLastProperty(the, fxNewSymbolInstance(the)); for (builder = gx_Symbol_prototype_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); slot = fxNextStringProperty(the, slot, "Symbol", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxSymbolPrototype = *the->stack; slot = fxLastProperty(the, fxNewHostConstructorGlobal(the, fx_Symbol, 0, mxID(_Symbol), XS_GET_ONLY)); for (builder = gx_Symbol_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_hasInstance), mxID(_hasInstance), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_isConcatSpreadable), mxID(_isConcatSpreadable), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_iterator), mxID(_iterator), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_match), mxID(_match), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_replace), mxID(_replace), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_search), mxID(_search), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_species), mxID(_species), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_split), mxID(_split), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_toPrimitive), mxID(_toPrimitive), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_toStringTag), mxID(_toStringTag), XS_GET_ONLY); slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_unscopables), mxID(_unscopables), XS_GET_ONLY); the->stack++; }
txID fxFindModule(txMachine* the, txID moduleID, txSlot* slot) { char name[PATH_MAX]; char base[PATH_MAX]; txBoolean absolute, relative, search; txSlot *key, *iterator, *result; txString dot, slash; txID id; fxToStringBuffer(the, slot, name, sizeof(name)); if ((!c_strncmp(name, "./", 2)) || (!c_strncmp(name, "../", 3))) { absolute = 0; relative = (moduleID == XS_NO_ID) ? 0 : 1; search = 0; } else if ((!c_strncmp(name, "/", 1))) { absolute = 1; relative = 0; search = 0; } else { absolute = 0; relative = (moduleID == XS_NO_ID) ? 0 : 1; search = 1; } slash = c_strrchr(name, '/'); if (!slash) slash = name; dot = c_strrchr(slash, '.'); if (!dot) dot = name + c_strlen(name); if (absolute) { if (fxFindURI(the, "", name, dot, &id)) return id; } if (relative) { key = fxGetKey(the, moduleID); c_strcpy(base, key->value.key.string); if (fxFindURI(the, base, name, dot, &id)) return id; } if (search) { mxCallID(&mxModulePaths, mxID(_Symbol_iterator), 0); iterator = the->stack; for (;;) { mxCallID(iterator, mxID(_next), 0); result = the->stack; mxGetID(result, mxID(_done)); if (fxToBoolean(the, the->stack)) break; the->stack++; mxGetID(result, mxID(_value)); fxToStringBuffer(the, the->stack++, base, sizeof(base)); if (fxFindURI(the, base, name, dot, &id)) return id; } } mxReferenceError("module \"%s\" not found", name); return XS_NO_ID; }
txSlot* fxNewHostFunction(txMachine* the, txCallback theCallback, txInteger theLength, txInteger name) { txSlot* instance; txSlot* property; mxPushUndefined(); instance = fxNewSlot(the); instance->flag = XS_VALUE_FLAG; instance->kind = XS_INSTANCE_KIND; instance->value.instance.garbage = C_NULL; instance->value.instance.prototype = mxFunctionPrototype.value.reference; the->stack->value.reference = instance; the->stack->kind = XS_REFERENCE_KIND; /* CALLBACK */ property = instance->next = fxNewSlot(the); property->flag = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; property->kind = XS_CALLBACK_KIND; property->value.callback.address = theCallback; property->value.callback.IDs = (txID*)the->code; /* CLOSURE */ property = property->next = fxNewSlot(the); property->flag = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; property->kind = XS_NULL_KIND; /* HOME */ property = property->next = fxNewSlot(the); property->flag = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; property->kind = XS_NULL_KIND; /* MODULE */ property = property->next = fxNewSlot(the); property->flag = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; if (the->frame && (mxFunction->kind == XS_REFERENCE_KIND)) { txSlot* slot = mxFunctionInstanceModule(mxFunction->value.reference); property->kind = slot->kind; property->value = slot->value; } else property->kind = XS_NULL_KIND; #ifdef mxProfile /* PROFILE */ property = property->next = fxNewSlot(the); property->flag = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; property->kind = XS_INTEGER_KIND; property->value.integer = the->profileID; the->profileID++; #endif /* LENGTH */ property = property->next = fxNewSlot(the); property->flag = XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; property->ID = mxID(_length); property->kind = XS_INTEGER_KIND; property->value.integer = theLength; /* NAME */ property = property->next = fxNewSlot(the); property->flag = XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG; property->ID = mxID(_name); property->kind = mxEmptyString.kind; property->value = mxEmptyString.value; if (name != XS_NO_ID) fxRenameFunction(the, property, name, "", C_NULL); return instance; }
void fx_Function_prototype_bind(txMachine* the) { txSlot* instance = fxToInstance(the, mxThis); txSize length; txSlot* slot; txID id; txSlot* arguments; txSlot* argument; txSize c = mxArgc, i; if (!fxIsFunction(the, instance)) mxTypeError("this is no Function instance"); if (fxHasOwnProperty(the, instance, mxID(_length))) { mxPushSlot(mxThis); fxGetID(the, mxID(_length)); length = fxToInteger(the, the->stack++); if (c > 1) length -= c - 1; if (length < 0) length = 0; mxPop(); } else length = 0; mxPushSlot(mxThis); fxGetID(the, mxID(_name)); mxPushStringC("bound "); fxConcatString(the, the->stack, the->stack + 1); slot = fxNewName(the, the->stack); id = slot->ID; mxPop(); mxPop(); mxPushReference(instance->value.instance.prototype); instance = fxNewFunctionInstance(the, id); mxPullSlot(mxResult); slot = mxFunctionInstanceCode(instance); slot->kind = XS_CALLBACK_KIND; slot->value.callback.address = fx_Function_prototype_bound; slot->value.callback.IDs = (txID*)mxIDs.value.code; slot = mxFunctionInstanceInfo(instance); slot->value.info.length = (txID)length; slot = fxLastProperty(the, instance); slot = fxNextSlotProperty(the, slot, mxThis, mxID(_boundFunction), XS_GET_ONLY); if (c > 0) slot = fxNextSlotProperty(the, slot, mxArgv(0), mxID(_boundThis), XS_GET_ONLY); else slot = fxNextUndefinedProperty(the, slot, mxID(_boundThis), XS_GET_ONLY); mxPush(mxArrayPrototype); arguments = fxNewArrayInstance(the); argument = arguments->next; for (i = 1; i < c; i++) { argument->next = fxNewSlot(the); argument = argument->next; argument->kind = mxArgv(i)->kind; argument->value = mxArgv(i)->value; } arguments->next->value.array.length = mxArgc - 1; fxCacheArray(the, arguments); slot = fxNextSlotProperty(the, slot, the->stack, mxID(_boundArguments), XS_GET_ONLY); mxPop(); }
mxArray * toMx(GPUtype *r, int isscalar=0) { // garbage collector //MyGCObj<GPUtype> mgc; //mgc.setPtr(r); mxArray *plhs[1]; mwSize dims[2] = {1,1}; if ((r->getNumel() == 1)||(isscalar==1)) { if (r->isComplex()) { if (r->getType()==gpuCFLOAT) { Complex tmpr; try { GPUopCudaMemcpy(&tmpr, r->getGPUptr(), GPU_SIZE_OF_CFLOAT * 1, cudaMemcpyDeviceToHost, r->getGPUmanager()); } catch (GPUexception ex) { mexErrMsgTxt(ex.getError()); } //plhs[0] = mxCreateDoubleMatrix(1, 1, mxCOMPLEX); plhs[0] = mxCreateNumericArray(2, dims, mxSINGLE_CLASS, mxCOMPLEX); memcpy (mxGetPr(plhs[0]), &tmpr.x, sizeof(float) ); memcpy (mxGetPi(plhs[0]), &tmpr.y, sizeof(float) ); //*mxGetPr(plhs[0]) = tmpr.x; //*mxGetPi(plhs[0]) = tmpr.y; } else if (r->getType()==gpuCDOUBLE){ DoubleComplex tmpr; try { GPUopCudaMemcpy(&tmpr, r->getGPUptr(), GPU_SIZE_OF_CDOUBLE * 1, cudaMemcpyDeviceToHost, r->getGPUmanager()); } catch (GPUexception ex) { mexErrMsgTxt(ex.getError()); } //plhs[0] = mxCreateDoubleMatrix(1, 1, mxCOMPLEX); plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxCOMPLEX); memcpy (mxGetPr(plhs[0]), &tmpr.x, sizeof(double) ); memcpy (mxGetPi(plhs[0]), &tmpr.y, sizeof(double) ); //*mxGetPr(plhs[0]) = tmpr.x; //*mxGetPi(plhs[0]) = tmpr.y; } } else { if (r->getType()==gpuFLOAT) { float tmpr = 0.0; try { GPUopCudaMemcpy(&tmpr, r->getGPUptr(), GPU_SIZE_OF_FLOAT * 1, cudaMemcpyDeviceToHost, r->getGPUmanager()); } catch (GPUexception ex) { mexErrMsgTxt(ex.getError()); } //plhs[0] = mxCreateDoubleScalar(tmpr); plhs[0] = mxCreateNumericArray(2, dims, mxSINGLE_CLASS, mxREAL); memcpy (mxGetPr(plhs[0]), &tmpr, sizeof(float) ); //*mxGetPr(plhs[0]) = tmpr; } else if (r->getType()==gpuDOUBLE) { double tmpr = 0.0; try { GPUopCudaMemcpy(&tmpr, r->getGPUptr(), GPU_SIZE_OF_DOUBLE * 1, cudaMemcpyDeviceToHost, r->getGPUmanager()); } catch (GPUexception ex) { mexErrMsgTxt(ex.getError()); } //plhs[0] = mxCreateDoubleScalar(tmpr); plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); memcpy (mxGetPr(plhs[0]), &tmpr, sizeof(double) ); //*mxGetPr(plhs[0]) = tmpr; } } // remove first from Garbage collector //mgc.remPtr(r); // must delete r delete r; } else { GPUmanager *gman = r->getGPUmanager(); int slot = 0; // request a free slot in cache gpuTYPE_t mxtype = gpuNOTDEF; // first I search for a cached element. mxtype is the tyep of cached // element I am looking for. // If the cached element is not found, I have to create a new GPUsingle/GPUdouble // which is not registered to the cache. if (r->isFloat()) mxtype = gpuFLOAT; // complex as well if (r->isDouble()) mxtype = gpuDOUBLE; // complex as well // NPN yet implemented //void *mxtmp = gman->extCacheGetFreeSlot(&slot, mxtype); void *mxtmp = gman->extCacheGetFreeSlot(&slot, gpuNOTDEF); if (slot<0) { // internal error mexErrMsgTxt(ERROR_MXID_CACHEINTERNAL); } if (mxtmp==NULL) { // the mxArray is not in cache mxArray *prhs[2]; prhs[0] = mxCreateDoubleScalar(slot); prhs[1] = prhs[0]; //mxArray *tmpr = toMxStruct(r); if (r->getType()==gpuCFLOAT) { mexCallMATLAB(1, plhs, 2, prhs, "GPUsingle"); } else if (r->getType()==gpuFLOAT){ mexCallMATLAB(1, plhs, 2, prhs, "GPUsingle"); } else if (r->getType()==gpuCDOUBLE){ mexCallMATLAB(1, plhs, 2, prhs, "GPUdouble"); } else if (r->getType()==gpuDOUBLE){ mexCallMATLAB(1, plhs, 2, prhs, "GPUdouble"); } gman->extCacheRegisterPtrBySlot(slot, mxID(plhs[0]), plhs[0], r, gpuNOTDEF); //mexPrintf("%p -> %p\n", plhs[0], r); // clean up mxDestroyArray(prhs[0]); } else { // shoud no be here, not impl. mexErrMsgTxt(ERROR_MXID_CACHEINTERNAL); plhs[0] = (mxArray *) mxtmp; gman->extCacheRegisterPtrBySlot(slot, mxID(plhs[0]), NULL, r, mxtype); } //mexPrintf("************** begin return mx ****************\n"); //gman->extCachePrint(); } // remove from gc //mgc.remPtr(r); mxArray * tmp = plhs[0]; //printMx1(tmp, 0); //mexPrintf("************** end return mx ****************\n"); //mexPrintf("<- %p\n",tmp->reserved2); //mexPrintf("<- %p\n",tmp->data.number_array.pdata); return plhs[0]; }
void fxBuildDate(txMachine* the) { static const txHostFunctionBuilder gx_Date_prototype_builders[] = { { fx_Date_prototype_getMilliseconds, 0, _getMilliseconds }, { fx_Date_prototype_getSeconds, 0, _getSeconds }, { fx_Date_prototype_getMinutes, 0, _getMinutes }, { fx_Date_prototype_getHours, 0, _getHours }, { fx_Date_prototype_getDay, 0, _getDay }, { fx_Date_prototype_getDate, 0, _getDate }, { fx_Date_prototype_getMonth, 0, _getMonth }, { fx_Date_prototype_getYear, 0, _getYear }, { fx_Date_prototype_getFullYear, 0, _getFullYear }, { fx_Date_prototype_getUTCMilliseconds, 0, _getUTCMilliseconds }, { fx_Date_prototype_getUTCSeconds, 0, _getUTCSeconds }, { fx_Date_prototype_getUTCMinutes, 0, _getUTCMinutes }, { fx_Date_prototype_getUTCHours, 0, _getUTCHours }, { fx_Date_prototype_getUTCDay, 0, _getUTCDay }, { fx_Date_prototype_getUTCDate, 0, _getUTCDate }, { fx_Date_prototype_getUTCMonth, 0, _getUTCMonth }, { fx_Date_prototype_getUTCFullYear, 0, _getUTCFullYear }, { fx_Date_prototype_valueOf, 0, _getTime }, { fx_Date_prototype_getTimezoneOffset, 0, _getTimezoneOffset }, { fx_Date_prototype_setMilliseconds, 1, _setMilliseconds }, { fx_Date_prototype_setSeconds, 1, _setSeconds }, { fx_Date_prototype_setMinutes, 1, _setMinutes }, { fx_Date_prototype_setHours, 1, _setHours }, { fx_Date_prototype_setDate, 1, _setDate }, { fx_Date_prototype_setMonth, 1, _setMonth }, { fx_Date_prototype_setYear, 1, _setYear }, { fx_Date_prototype_setFullYear, 1, _setFullYear }, { fx_Date_prototype_setTime, 1, _setTime }, { fx_Date_prototype_setUTCMilliseconds, 1, _setUTCMilliseconds }, { fx_Date_prototype_setUTCSeconds, 1, _setUTCSeconds }, { fx_Date_prototype_setUTCMinutes, 1, _setUTCMinutes }, { fx_Date_prototype_setUTCHours, 1, _setUTCHours }, { fx_Date_prototype_setUTCDate, 1, _setUTCDate }, { fx_Date_prototype_setUTCMonth, 1, _setUTCMonth }, { fx_Date_prototype_setUTCFullYear, 1, _setUTCFullYear }, { fx_Date_prototype_toDateString, 0, _toDateString }, { fx_Date_prototype_toUTCString, 0, _toGMTString }, { fx_Date_prototype_toISOString, 0, _toISOString }, { fx_Date_prototype_toJSON, 1, _toJSON }, { fx_Date_prototype_toDateString, 0, _toLocaleDateString }, { fx_Date_prototype_toString, 0, _toLocaleString }, { fx_Date_prototype_toTimeString, 0, _toLocaleTimeString }, { fx_Date_prototype_toPrimitive, 0, _Symbol_toPrimitive }, { fx_Date_prototype_toString, 0, _toString }, { fx_Date_prototype_toTimeString, 0, _toTimeString }, { fx_Date_prototype_toUTCString, 0, _toUTCString }, { fx_Date_prototype_valueOf, 0, _valueOf }, { C_NULL, 0, 0 }, }; static const txHostFunctionBuilder gx_Date_builders[] = { { fx_Date_now, 0, _now }, { fx_Date_parse, 1, _parse }, { fx_Date_UTC, 2, _UTC }, { C_NULL, 0, 0 }, }; const txHostFunctionBuilder* builder; txSlot* slot; txTimeDescription td; mxPush(mxObjectPrototype); slot = fxLastProperty(the, fxNewDateInstance(the)); for (builder = gx_Date_prototype_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); slot = fxNextStringProperty(the, slot, "Date", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); mxDatePrototype = *the->stack; slot = fxLastProperty(the, fxNewHostConstructorGlobal(the, fx_Date, 1, mxID(_Date), XS_GET_ONLY)); for (builder = gx_Date_builders; builder->callback; builder++) slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG); the->stack++; c_memset(&td, 0, sizeof(txTimeDescription)); td.tm.tm_mday = 2; td.tm.tm_year = 70; td.tm.tm_isdst = -1; gxDeltaTime = 1000.0 * ((long)c_mktime(&(td.tm)) - (24L * 3600L)); gxMaxTime = TIME_2100; //extend from LONG_MAX to this number to support year upto 2099 gxMinTime = LONG_MIN; }
void fxBuildError(txMachine* the) { txSlot* slot; mxPush(mxObjectPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextHostFunctionProperty(the, slot, fx_Error_toString, 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG); slot = fxNextHostFunctionProperty(the, slot, fx_Error_toString, 0, mxID(_toString), XS_DONT_ENUM_FLAG); slot = fxNextStringProperty(the, slot, "Error", mxID(_name), XS_DONT_ENUM_FLAG); slot = fxNextStringProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG); mxErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_Error, 0, mxID(_Error), XS_GET_ONLY); the->stack++; mxPush(mxErrorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextStringProperty(the, slot, "EvalError", mxID(_name), XS_DONT_ENUM_FLAG); mxEvalErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_EvalError, 1, mxID(_EvalError), XS_GET_ONLY); the->stack++; mxPush(mxErrorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextStringProperty(the, slot, "RangeError", mxID(_name), XS_DONT_ENUM_FLAG); mxRangeErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_RangeError, 1, mxID(_RangeError), XS_GET_ONLY); the->stack++; mxPush(mxErrorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextStringProperty(the, slot, "ReferenceError", mxID(_name), XS_DONT_ENUM_FLAG); mxReferenceErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_ReferenceError, 1, mxID(_ReferenceError), XS_GET_ONLY); the->stack++; mxPush(mxErrorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextStringProperty(the, slot, "SyntaxError", mxID(_name), XS_DONT_ENUM_FLAG); mxSyntaxErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_SyntaxError, 1, mxID(_SyntaxError), XS_GET_ONLY); the->stack++; mxPush(mxErrorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextStringProperty(the, slot, "TypeError", mxID(_name), XS_DONT_ENUM_FLAG); mxTypeErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_TypeError, 1, mxID(_TypeError), XS_GET_ONLY); the->stack++; mxPush(mxErrorPrototype); slot = fxLastProperty(the, fxNewObjectInstance(the)); slot = fxNextStringProperty(the, slot, "URIError", mxID(_name), XS_DONT_ENUM_FLAG); mxURIErrorPrototype = *the->stack; fxNewHostConstructorGlobal(the, fx_URIError, 1, mxID(_URIError), XS_GET_ONLY); the->stack++; }