//--------------------------------------------------------------------------- // TJSGetExceptionObject : retrieves TJS 'Exception' object //--------------------------------------------------------------------------- void TJSGetExceptionObject(tTJS *tjs, tTJSVariant *res, tTJSVariant &msg, tTJSVariant *trace/* trace is optional */) { if(!res) return; // not prcess // retrieve class "Exception" from global iTJSDispatch2 *global = tjs->GetGlobalNoAddRef(); tTJSVariant val; static tTJSString Exception_name(TJS_W("Exception")); tjs_error hr = global->PropGet(0, Exception_name.c_str(), Exception_name.GetHint(), &val, global); if(TJS_FAILED(hr)) TJS_eTJSError(TJSExceptionNotFound); // create an Exception object iTJSDispatch2 *excpobj; tTJSVariantClosure clo = val.AsObjectClosureNoAddRef(); tTJSVariant *pmsg = &msg; hr = clo.CreateNew(0, NULL, NULL, &excpobj, 1, &pmsg, clo.ObjThis); if(TJS_FAILED(hr)) TJS_eTJSError(TJSExceptionNotFound); if(trace) { static tTJSString trace_name(TJS_W("trace")); excpobj->PropSet(TJS_MEMBERENSURE, trace_name.c_str(), trace_name.GetHint(), trace, excpobj); } *res = tTJSVariant(excpobj, excpobj); excpobj->Release(); }
void tTJSBinarySerializer::InsertArray( tTJSArrayObject* array, tjs_uint index, tTJSVariant* value ) { if( value == NULL ) TJS_eTJSError( TJSReadError ); tTJSArrayNI* ni = NULL; tjs_error hr = array->NativeInstanceSupport(TJS_NIS_GETINSTANCE, TJSGetArrayClassID(), (iTJSNativeInstance**)&ni ); if( TJS_SUCCEEDED(hr) ) { // array->Insert( ni, *value, index ); array->Add( ni, *value ); } }
//--------------------------------------------------------------------------- void TJSThrowFrom_tjs_error(tjs_error hr, const tjs_char *name) { // raise an exception descripted as tjs_error // name = variable name ( otherwide it can be NULL ) switch(hr) { case TJS_E_MEMBERNOTFOUND: { if(name) { ttstr str(TJSMemberNotFound); str.Replace(TJS_W("%1"), name); TJS_eTJSError(str); } else { TJS_eTJSError(TJSMemberNotFoundNoNameGiven); } } case TJS_E_NOTIMPL: TJS_eTJSError(TJSNotImplemented); case TJS_E_INVALIDPARAM: TJS_eTJSError(TJSInvalidParam); case TJS_E_BADPARAMCOUNT: TJS_eTJSError(TJSBadParamCount); case TJS_E_INVALIDTYPE: TJS_eTJSError(TJSInvalidType); case TJS_E_ACCESSDENYED: TJS_eTJSError(TJSAccessDenyed); case TJS_E_INVALIDOBJECT: TJS_eTJSError(TJSInvalidObject); case TJS_E_NATIVECLASSCRASH: TJS_eTJSError(TJSNativeClassCrash); default: if(TJS_FAILED(hr)) { tjs_char buf[256]; TJS_snprintf(buf, 256, TJS_W("Unknown failure : %08X"), hr); TJS_eTJSError(buf); } } }
tTJSVariant* tTJSBinarySerializer::ReadDictionary( const tjs_uint8* buff, const tjs_uint size, const tjs_uint count, tjs_uint& index ) { if( index > size ) return NULL; tTJSDictionaryObject* dic = CreateDictionary( count ); for( tjs_uint i = 0; i < count; i++ ) { tjs_uint8 type = buff[index]; index++; // 最初に文字を読む tTJSVariantString* name = NULL; switch( type ) { case TYPE_STRING8: { if( (index+sizeof(tjs_uint8)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint8 len = buff[index]; index++; if( (index+(len*sizeof(tjs_char))) > size ) TJS_eTJSError( TJSReadError ); name = ReadString( buff, len, index ); break; } case TYPE_STRING16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 len = Read16( buff, index ); if( (index+(len*sizeof(tjs_char))) > size ) TJS_eTJSError( TJSReadError ); name = ReadString( buff, len, index ); break; } case TYPE_STRING32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 len = Read32( buff, index ); if( (index+(len*sizeof(tjs_char))) > size ) TJS_eTJSError( TJSReadError ); name = ReadString( buff, len, index ); break; } default: if( type >= TYPE_FIX_STRING_MIN && type <= TYPE_FIX_STRING_MAX ) { tjs_int len = type - TYPE_FIX_STRING_MIN; if( (len*sizeof(tjs_char)+index) > size ) TJS_eTJSError( TJSReadError ); name = ReadString( buff, len, index ); } else { // Dictionary形式の場合、最初に文字列がこないといけない TJS_eTJSError( TJSReadError ); } break; } // 次に要素を読む tTJSVariant* value = ReadBasicType( buff, size, index ); AddDictionary( dic, name, value ); delete value; name->Release(); } tTJSVariant* ret = new tTJSVariant( dic, dic ); dic->Release(); return ret; }
//--------------------------------------------------------------------------- void tTJSScriptBlock::PushContextStack(const tjs_char *name, tTJSContextType type) { tTJSInterCodeContext *cntx; cntx = new tTJSInterCodeContext(InterCodeContext, name, this, type); if(InterCodeContext==NULL) { if(TopLevelContext) TJS_eTJSError(TJSInternalError); TopLevelContext = cntx; TopLevelContext->AddRef(); } ContextStack.push(cntx); InterCodeContext = cntx; }
tTJSVariant* tTJSBinarySerializer::Read( tTJSBinaryStream* stream ) { tjs_uint64 pos = stream->GetPosition(); tjs_uint size = (tjs_uint)( stream->GetSize() - pos ); tjs_uint8* buffstart = new tjs_uint8[size]; if( size != stream->Read( buffstart, size ) ) { TJS_eTJSError( TJSReadError ); } tjs_uint index = 0; tTJSVariant* ret = ReadBasicType( buffstart, size, index ); delete[] buffstart; return ret; }
void Console::throwMsg(DEBUG_MSG error,const String& p1) { String msg; switch (error) { case ERROR_IMAGE_LOAD_FAILD: msg = L"图片资源加载失败:"+p1; break; case ERROR_FILE_NOT_EXIST: msg = L"文件未找到:"+p1; break; case ERROR_ADDFONT_FAILD: msg = L"添加字体时出错"; break; default: msg = L"未知错误"; break; } TJS_eTJSError(msg); }
void Console::throwMsg(DEBUG_MSG error,int p1,const String& p2) { String msg; switch (error) { case ERROR_KAG_UNKONW: msg = String::fromFormat(L"(#%d)KAG解析发生未知错误",p1); break; case ERROR_KAG_LABELKEY_NULL: msg = String::fromFormat(L"(#%d)标签的名字不能为空",p1); break; case ERROR_KAG_VALUE_STRING_ENDED: msg = String::fromFormat(L"(#%d)字符串没有结尾,可能缺少 \" 或 \' ,请检查",p1); break; case ERROR_KAG_TAG_ENDED: msg = String::fromFormat(L"(#%d)Tag没有结尾,可能缺少 ] ,请检查",p1); break; case ERROR_KAG_LABEL_FIND_FAIL: msg = String::fromFormat(L"(#%d)未找到名为%ls的标签",p1,p2.c_wstr()); break; case ERROR_KAG_TAG_FIND_FAIL: msg = String::fromFormat(L"(#%d)未找到名为%ls的指令",p1); break; case ERROR_KAG_TOO_MANY_RETURN: msg = String::fromFormat(L"(#%d)过多的Return指令,与Call指令无法形成对应",p1); break; case ERROR_KAG_MACRONAME_EMPTY: msg = String::fromFormat(L"(#%d)Macro的name属性为空",p1); break; case ERROR_KAG_MACRO_NESTING: msg = String::fromFormat(L"(#%d)Macro不可嵌套",p1); break; case ERROR_KAG_IF_FAIL: msg = String::fromFormat(L"(#%d)if、elsif、else、endif不对应",p1); break; default: msg = String::fromFormat(L"(#%d)KAG发生未知错误",p1); break; } TJS_eTJSError(msg); }
//--------------------------------------------------------------------------- void tTJSDictionaryNI::AssignStructure(iTJSDispatch2 * dsp, std::vector<iTJSDispatch2 *> &stack) { // assign structured data from dsp tTJSArrayNI *dicni = NULL; if(TJS_SUCCEEDED(dsp->NativeInstanceSupport(TJS_NIS_GETINSTANCE, ClassID_Dictionary, (iTJSNativeInstance**)&dicni)) ) { // copy from dictionary stack.push_back(dsp); try { Owner->Clear(); // reserve area tSaveMemberCountCallback countCallback; dsp->EnumMembers(TJS_IGNOREPROP, &tTJSVariantClosure(&countCallback, NULL), dsp); tjs_int reqcount = countCallback.Count + Owner->Count; Owner->RebuildHash( reqcount ); tAssignStructCallback callback; callback.Dest = Owner; callback.Stack = &stack; dsp->EnumMembers(TJS_IGNOREPROP, &tTJSVariantClosure(&callback, NULL), dsp); } catch(...) { stack.pop_back(); throw; } stack.pop_back(); } else { TJS_eTJSError(TJSSpecifyDicOrArray); } }
//--------------------------------------------------------------------------- void tTJSBinaryStream::ReadBuffer(void *buffer, tjs_uint read_size) { if(Read(buffer, read_size) != read_size) TJS_eTJSError(TJSReadError); }
void tTJSBinarySerializer::AddDictionary( tTJSDictionaryObject* dic, tTJSVariantString* name, tTJSVariant* value ) { if( name == NULL || value == NULL ) TJS_eTJSError( TJSReadError ); dic->PropSetByVS( TJS_MEMBERENSURE, name, value, dic ); }
void Console::throwMsg(const String& v) { TJS_eTJSError(v); }
tTJSVariant* tTJSBinarySerializer::ReadBasicType( const tjs_uint8* buff, const tjs_uint size, tjs_uint& index ) { if( index > size ) return NULL; tjs_uint8 type = buff[index]; index++; switch( type ) { case TYPE_NIL: return new tTJSVariant((iTJSDispatch2*)NULL); case TYPE_VOID: return new tTJSVariant(); case TYPE_TRUE: return new tTJSVariant((tjs_int)1); case TYPE_FALSE: return new tTJSVariant((tjs_int)0); case TYPE_STRING8: { if( (index+sizeof(tjs_uint8)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint8 len = buff[index]; index++; if( (index+(len*sizeof(tjs_char))) > size ) TJS_eTJSError( TJSReadError ); return ReadStringVarint( buff, len, index ); } case TYPE_STRING16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 len = Read16( buff, index ); if( (index+(len*sizeof(tjs_char))) > size ) TJS_eTJSError( TJSReadError ); return ReadStringVarint( buff, len, index ); } case TYPE_STRING32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 len = Read32( buff, index ); if( (index+(len*sizeof(tjs_char))) > size ) TJS_eTJSError( TJSReadError ); return ReadStringVarint( buff, len, index ); } case TYPE_FLOAT: { if( (index+sizeof(float)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 t = Read32( buff, index ); return new tTJSVariant(*(float*)&t); } case TYPE_DOUBLE: { if( (index+sizeof(double)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint64 t = Read64( buff, index ); return new tTJSVariant(*(double*)&t); } case TYPE_UINT8: { if( (index+sizeof(tjs_uint8)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint8 t = buff[index]; index++; return new tTJSVariant( t ); } case TYPE_UINT16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 t = Read16( buff, index ); return new tTJSVariant( t ); } case TYPE_UINT32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 t = Read32( buff, index ); return new tTJSVariant( (tjs_int64)t ); } case TYPE_UINT64: { if( (index+sizeof(tjs_uint64)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint64 t = Read64( buff, index ); return new tTJSVariant( (tjs_int64)t ); } case TYPE_INT8: { if( (index+sizeof(tjs_uint8)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint8 t = buff[index]; index++; return new tTJSVariant( (tjs_int8)t ); } case TYPE_INT16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 t = Read16( buff, index ); return new tTJSVariant( (tjs_int16)t ); } case TYPE_INT32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 t = Read32( buff, index ); return new tTJSVariant( (tjs_int32)t ); } case TYPE_INT64: { if( (index+sizeof(tjs_uint64)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint64 t = Read64( buff, index ); return new tTJSVariant( (tjs_int64)t ); } case TYPE_RAW16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 len = Read16( buff, index ); if( (index+len) > size ) TJS_eTJSError( TJSReadError ); return ReadOctetVarint( buff, len, index ); } case TYPE_RAW32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 len = Read32( buff, index ); if( (index+len) > size ) TJS_eTJSError( TJSReadError ); return ReadOctetVarint( buff, len, index ); } case TYPE_ARRAY16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 count = Read16( buff, index ); return ReadArray( buff, size, count, index ); } case TYPE_ARRAY32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 count = Read32( buff, index ); return ReadArray( buff, size, count, index ); } case TYPE_MAP16: { if( (index+sizeof(tjs_uint16)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint16 count = Read16( buff, index ); return ReadDictionary( buff, size, count, index ); } case TYPE_MAP32: { if( (index+sizeof(tjs_uint32)) > size ) TJS_eTJSError( TJSReadError ); tjs_uint32 count = Read32( buff, index ); return ReadDictionary( buff, size, count, index ); } default: { if( type >= TYPE_POSITIVE_FIX_NUM_MIN && type <= TYPE_POSITIVE_FIX_NUM_MAX ) { tjs_int value = type; return new tTJSVariant(value); } else if( type >= TYPE_NEGATIVE_FIX_NUM_MIN && type <= TYPE_NEGATIVE_FIX_NUM_MAX ) { tjs_int value = type; return new tTJSVariant(value); } else if( type >= TYPE_FIX_RAW_MIN && type <= TYPE_FIX_RAW_MAX ) { // octet tjs_int len = type - TYPE_FIX_RAW_MIN; if( (len*sizeof(tjs_uint8)+index) > size ) TJS_eTJSError( TJSReadError ); return ReadOctetVarint( buff, len, index ); } else if( type >= TYPE_FIX_STRING_MIN && type <= TYPE_FIX_STRING_MAX ) { tjs_int len = type - TYPE_FIX_STRING_MIN; if( (len*sizeof(tjs_char)+index) > size ) TJS_eTJSError( TJSReadError ); return ReadStringVarint( buff, len, index ); } else if( type >= TYPE_FIX_ARRAY_MIN && type <= TYPE_FIX_ARRAY_MAX ) { tjs_int count = type - TYPE_FIX_ARRAY_MIN; return ReadArray( buff, size, count, index ); } else if( type >= TYPE_FIX_MAP_MIN && type <= TYPE_FIX_MAP_MAX ) { tjs_int count = type - TYPE_FIX_MAP_MIN; return ReadDictionary( buff, size, count, index ); } else { TJS_eTJSError( TJSReadError ); return NULL; } } } }
//--------------------------------------------------------------------------- // tTJSDateParser : A date/time parser class //--------------------------------------------------------------------------- tTJSDateParser::tTJSDateParser(const tjs_char *in) { Input = InputPointer = in; YearSet = MonthSet = MDaySet = HourSet = MinSet = SecSet = TimeZoneSet = TimeZoneOffsetSet = AMPMSet = false; if(dpparse(this)) TJS_eTJSError(TJSCannotParseDate); // currently no omissions is allowed except for Secs if(!YearSet || !MonthSet || !MDaySet || !HourSet || !MinSet) TJS_eTJSError(TJSCannotParseDate); if(!SecSet) SecSet = true, Sec = 0; // convert Timezone/TimezoneOffset to time_t representation if(TimeZoneSet) { // input timezone is [+/-]hhmm bool sign = TimeZone < 0 ? true : false; if(sign) TimeZone = -TimeZone; TimeZone = (int)(TimeZone / 100) * 60*60 + (TimeZone % 100) * 60; if(sign) TimeZone = -TimeZone; } if(TimeZoneOffsetSet) { // input timezone is [+/-]hhmm bool sign = TimeZoneOffset < 0 ? true : false; if(sign) TimeZoneOffset = -TimeZoneOffset; TimeZoneOffset = (int)(TimeZoneOffset / 100) * 60*60 + (TimeZoneOffset % 100) * 60; if(sign) TimeZoneOffset = -TimeZoneOffset; } // Timezone is default system timezone when timezone is omitted. if(!TimeZoneSet && !TimeZoneOffsetSet) { TimeZoneSet = true; TimeZone = -TJS_timezone; } // Adjust AM/PM if(AMPMSet && AMPM) Hour += 12; // build tm structure struct tm stm; memset(&stm, 0, sizeof(stm)); stm.tm_year = Year - 1900; stm.tm_mon = Month; stm.tm_mday = MDay; stm.tm_hour = Hour; stm.tm_min = Min; stm.tm_sec = Sec; time_t tmv = mktime(&stm); if(tmv == -1) TJS_eTJSError(TJSInvalidValueForTimestamp); // adjust time zone tmv -= TJS_timezone; tjs_int tz = 0; if(TimeZoneSet) tz += TimeZone; if(TimeZoneOffsetSet) tz += TimeZoneOffset; tmv -= tz; // store result Time = (tjs_int64)tmv * 1000; }
//--------------------------------------------------------------------------- void tTJSBinaryStream::WriteBuffer(const void *buffer, tjs_uint write_size) { if(Write(buffer, write_size) != write_size) TJS_eTJSError(TJSWriteError); }
//--------------------------------------------------------------------------- void tTJSBinaryStream::SetPosition(tjs_uint64 pos) { if(pos != Seek(pos, TJS_BS_SEEK_SET)) TJS_eTJSError(TJSSeekError); }
void TJS_INTF_METHOD tTJSBinaryStream::SetEndOfStorage() { TJS_eTJSError(TJSWriteError); }