TITANIUM_FUNCTION(File, modifiedAt) { const auto ctx = get_context(); const std::vector<JSValue> dateArg = { ctx.CreateNumber(static_cast<double>(modificationTimestamp().count())) }; return ctx.CreateDate(dateArg); }
TITANIUM_FUNCTION(File, modificationTimestamp) { const auto ctx = get_context(); ctx.JSEvaluateScript("Ti.API.warn('modificationTimestamp() has been deprecated in 7.2.0 in favor of modifiedAt() to avoid platform-differences for return type. modifiedAt() will return a Date object on all platforms.');"); return ctx.CreateNumber(static_cast<double>(modificationTimestamp().count())); }
TITANIUM_PROPERTY_GETTER(Page, NavigationCacheMode) { auto value = unwrap()->NavigationCacheMode; auto context = get_context(); auto result = context.CreateNumber(static_cast<int32_t>(static_cast<int>(value))); // FIXME What if the enum isn't an int based one?! return result; }
Field::Field(int n) : d_impl_p(0) { if(!s_currentRep_p) { if(s_inInitialize) { s_currentRep_p = CreateNumber(n,1); } else { cerr << "Error with field setting\n"; }; }; RECORDHERE(d_impl_p = new FieldImplementation(s_currentRep_p->make(n),Adopt());) };
TITANIUM_FUNCTION(Properties, getInt) { ENSURE_STRING_AT_INDEX(property, 0); ENSURE_OPTIONAL_DOUBLE_AT_INDEX(defaultValue, 1, 0); const auto js_context = this_object.get_context(); const auto object_ptr = GetStaticObject(js_context).GetPrivate<Properties>(); return js_context.CreateNumber(object_ptr->getInt(property, defaultValue)); }
JSON *Duplicate(JSON *obj, int recurse){ switch (obj->type){ case JSON_NULL: return CreateNULL(); case JSON_TRUE: return CreateTrue(); case JSON_FALSE: return CreateFalse(); case JSON_NUMBER: return CreateNumber(obj->valuedouble); case JSON_STRING: return CreateString(obj->valuestring); case JSON_ARRAY:{ JSON *new_obj = CreateArray(); if (recurse == 0) new_obj->son = obj->son; else{ if (obj->son != NULL){ new_obj->son = Duplicate(obj->son, 1); JSON *old_item = obj->son, *new_item = new_obj->son; for ( ; old_item->next != NULL; old_item = old_item->next, new_item = new_item->next){ JSON *temp = Duplicate(old_item->next,1); new_item->next = temp; temp->last = new_item; } } } return new_obj; } case JSON_OBJECT:{ JSON *new_obj = CreateObject(); if (recurse == 0) new_obj->son = obj->son; else{ if (obj->son != NULL){ new_obj->son = Duplicate(obj->son, 1); JSON *old_item = obj->son, *new_item = new_obj->son; for ( ; old_item->next != NULL; old_item = old_item->next, new_item = new_item->next){ JSON *temp = Duplicate(old_item->next,1); new_item->next = temp; temp->last = new_item; } } } return new_obj; } case JSON_ENTRY:{ JSON *new_obj = CreateEntry(obj->key, Duplicate(obj->value,1)); return new_obj; } default: return NULL; } }
int TestSomeFuncs() { int score = 10; // 调用同学们实现的接口 JSON *root = CreateObject(); // 评测1 是否能正确创建Object if (root->type != JSON_OBJECT) { // 类型不对 score -= 2; } AddItemToObject(root, "name", CreateString("Hello World")); // 评测2 是否能正确AddItemToObject并且读取 JSON *value = GetItemInObject(root, "name"); // 类型不对或者值不对 if (value->type != JSON_STRING || strcmp(value->valuestring, "Hello World")) { score -= 2; } JSON *array = CreateArray(); AddItemToArray(array, CreateBool(0)); AddItemToArray(array, CreateNumber(2.3)); // 评测3 是否能正确AddItemToArray并且读取 JSON *item = GetItemInArray(array, 1); if (item->type != JSON_NUMBER || item->valuedouble != 2.3) { score -= 2; } AddItemToObject(root, "array", courses); // 现在root的状态 /* { "name": "Hello Wrold", "array": [ false, 2.3 ] } */ // 评测4 是否能正确地根据路径读取值 item = GetItemInJSON(root, "/array/0"); if (item->type != JSON_FALSE) { score -= 2; } PrintJSONToFile(root, "test.json"); // 评测5 是否与标准答案文件相同 if (!IsSame("test.json", "test.json.ans")) { score -= 2; } return score; }
JSON *Duplicate(JSON *item, int recurse) { JSON *rtn = NULL; JSON *iter = NULL; unsigned int i = 0; if (item == NULL) { printf("Exception: Cannot duplicate a NULL JSON pointer.\n"); return rtn; } switch (item->type) { case JSON_NULL: rtn = CreateNULL(); return rtn; case JSON_FALSE: rtn = CreateFalse(); return rtn; case JSON_TRUE: rtn = CreateTrue(); return rtn; case JSON_NUMBER: rtn = CreateNumber(item->valuedouble); return rtn; case JSON_STRING: rtn = CreateString(item->valuestring); return rtn; case JSON_ARRAY: rtn = CreateArray(); if (recurse == 1) { iter = item->childstart; for (i = 0; i < item->childlength; ++i) { AddItemToArray(rtn, Duplicate(iter, 1)); iter = iter->next; } } else { rtn->childlength = item->childlength; rtn->childstart = item->childstart; rtn->childend = item->childend; } return rtn; case JSON_OBJECT: rtn = CreateObject(); if (recurse == 1) { iter = item->childstart; for (i = 0; i < item->childlength; ++i) { AddItemToObject(rtn, iter->key, Duplicate(iter, 1)); iter = iter->next; } } else { rtn->childlength = item->childlength; rtn->childstart = item->childstart; rtn->childend = item->childend; } return rtn; } }
MovingAnimator* NumbersHolder::DisplayNumber(Dim number, Dim x){ MovingAnimator* g; if(suspending[number].size() == 0) CreateNumber(number); assert(suspending[number].size()); g = suspending[number].back(); g->GetSprite()->SetX(x); running[number].push_back(g); AnimatorHolder::Register( g ); AnimatorHolder::MarkAsRunning(g); suspending[number].pop_back(); return g; }
JSON *num_type_parse(){ double val = 0; int dp_found = 0; double div = 0; while ((str_value()>='0'&& str_value()<='9')|| str_value()=='.'){ // decimal point if (str_value() == '.'){ assert(dp_found==0); dp_found = 1; div = 1; } // digital else{ div *= 10; val = val * 10 + (str_value()-'0'); } next_char(); } if (dp_found) val /= div; return CreateNumber(val); }
TITANIUM_FUNCTION(Canvas, GetZIndex) { auto context = get_context(); if (arguments.size() == 1) { auto _0 = arguments.at(0); TITANIUM_ASSERT_AND_THROW(_0.IsObject(), "Expected Object"); auto object_element = static_cast<JSObject>(_0); auto wrapper_element = object_element.GetPrivate<Windows::UI::Xaml::UIElement>(); // FIXME What if the type we want here is some parent class of the actual wrapper's class? I think we'll get nullptr here. // We need some way to know the underlying type the JSObject maps to, get that, then cast to the type we want... auto element = wrapper_element->unwrapWindows_UI_Xaml_UIElement(); auto method_result = ::Windows::UI::Xaml::Controls::Canvas::GetZIndex(element); auto result = context.CreateNumber(method_result); return result; } // Catch-all if no arg count matches! TITANIUM_LOG_DEBUG("No method signature matched Canvas::GetZIndex with # of args: ", arguments.size()); return context.CreateUndefined(); }
Field::Field(const INTEGER & num,const INTEGER & den) : d_impl_p(0) { RECORDHERE(d_impl_p = new FieldImplementation(CreateNumber(num,den),Adopt::s_dummy);) };
JSON *ParseJSON(const char *value) { JSON *rtn = NULL; StrSlice *ss = NULL; StrSlice *iter = NULL; int len = strlen(value); int index = 0; char *value_deletespace = DeleteSpaces(value); switch(value[0]) { case '-': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': //To number return CreateNumber(FormatNumber(value_deletespace)); free(value_deletespace); break; case '\"': // To string return CreateString(FormatString(value)); break; case '{': rtn = CreateObject(); ss = GetObjectSlices(value); iter = ss; if (ss->length == 0) { DeleteStrSlice(ss); return rtn; } while (iter != NULL) { AddItemToObject(rtn, FormatString(iter->str), ParseJSON(DeleteSpaces(iter->next->str))); iter = iter->next->next; } DeleteStrSlice(ss); return rtn; break; case '[': rtn = CreateArray(); ss = GetArraySlices(value); iter = ss; if (ss->length == 0) { DeleteStrSlice(ss); return rtn; } while (iter != NULL) { AddItemToArray(rtn, ParseJSON(DeleteSpaces(iter->str))); iter = iter->next; } DeleteStrSlice(ss); return rtn; break; case 'n': if (strcmp(value_deletespace, "null") == 0) { free(value_deletespace); return CreateNULL(); } else { printf("Exception: Invalid Syntax \"%s\"", value); return NULL; } case 't': if (strcmp(value_deletespace, "true") == 0) { free(value_deletespace); return CreateTrue(); } else { printf("Exception: Invalid Syntax \"%s\"", value); return NULL; } break; case 'f': if (strcmp(value_deletespace, "false") == 0) { free(value_deletespace); return CreateFalse(); } else { printf("Exception: Invalid Syntax \"%s\"", value); return NULL; } break; } }
TITANIUM_FUNCTION(GeneralTransform, TransformBounds) { auto context = get_context(); if (arguments.size() == 1) { auto _0 = arguments.at(0); TITANIUM_ASSERT_AND_THROW(_0.IsObject(), "Expected Object"); auto object_rect = static_cast<JSObject>(_0); ::Windows::Foundation::Rect rect; // Assign fields explicitly since we didn't use a constructor auto object_rect_X = object_rect.GetProperty("X"); TITANIUM_ASSERT_AND_THROW(object_rect_X.IsNumber(), "Expected Number"); auto object_rect_X_ = static_cast<float>(static_cast<double>(object_rect_X)); rect.X = object_rect_X_; auto object_rect_Y = object_rect.GetProperty("Y"); TITANIUM_ASSERT_AND_THROW(object_rect_Y.IsNumber(), "Expected Number"); auto object_rect_Y_ = static_cast<float>(static_cast<double>(object_rect_Y)); rect.Y = object_rect_Y_; auto object_rect_Width = object_rect.GetProperty("Width"); TITANIUM_ASSERT_AND_THROW(object_rect_Width.IsNumber(), "Expected Number"); auto object_rect_Width_ = static_cast<float>(static_cast<double>(object_rect_Width)); rect.Width = object_rect_Width_; auto object_rect_Height = object_rect.GetProperty("Height"); TITANIUM_ASSERT_AND_THROW(object_rect_Height.IsNumber(), "Expected Number"); auto object_rect_Height_ = static_cast<float>(static_cast<double>(object_rect_Height)); rect.Height = object_rect_Height_; auto method_result = unwrap()->TransformBounds(rect); auto result = context.CreateObject(); auto method_result_X_ = context.CreateNumber(static_cast<double>(method_result.X)); result.SetProperty("X", method_result_X_); auto method_result_Y_ = context.CreateNumber(static_cast<double>(method_result.Y)); result.SetProperty("Y", method_result_Y_); auto method_result_Width_ = context.CreateNumber(static_cast<double>(method_result.Width)); result.SetProperty("Width", method_result_Width_); auto method_result_Height_ = context.CreateNumber(static_cast<double>(method_result.Height)); result.SetProperty("Height", method_result_Height_); return result; } // Catch-all if no arg count matches! TITANIUM_LOG_DEBUG("No method signature matched GeneralTransform::TransformBounds with # of args: ", arguments.size()); return context.CreateUndefined(); }
TITANIUM_FUNCTION(GeneralTransform, TryTransform) { auto context = get_context(); if (arguments.size() == 2) { auto _0 = arguments.at(0); TITANIUM_ASSERT_AND_THROW(_0.IsObject(), "Expected Object"); auto object_inPoint = static_cast<JSObject>(_0); ::Windows::Foundation::Point inPoint; // Assign fields explicitly since we didn't use a constructor auto object_inPoint_X = object_inPoint.GetProperty("X"); TITANIUM_ASSERT_AND_THROW(object_inPoint_X.IsNumber(), "Expected Number"); auto object_inPoint_X_ = static_cast<float>(static_cast<double>(object_inPoint_X)); inPoint.X = object_inPoint_X_; auto object_inPoint_Y = object_inPoint.GetProperty("Y"); TITANIUM_ASSERT_AND_THROW(object_inPoint_Y.IsNumber(), "Expected Number"); auto object_inPoint_Y_ = static_cast<float>(static_cast<double>(object_inPoint_Y)); inPoint.Y = object_inPoint_Y_; auto _1 = arguments.at(1); TITANIUM_ASSERT_AND_THROW(_1.IsObject(), "Expected Object"); auto object_outPoint = static_cast<JSObject>(_1); ::Windows::Foundation::Point outPoint; // Assign fields explicitly since we didn't use a constructor auto object_outPoint_X = object_outPoint.GetProperty("X"); TITANIUM_ASSERT_AND_THROW(object_outPoint_X.IsNumber(), "Expected Number"); auto object_outPoint_X_ = static_cast<float>(static_cast<double>(object_outPoint_X)); outPoint.X = object_outPoint_X_; auto object_outPoint_Y = object_outPoint.GetProperty("Y"); TITANIUM_ASSERT_AND_THROW(object_outPoint_Y.IsNumber(), "Expected Number"); auto object_outPoint_Y_ = static_cast<float>(static_cast<double>(object_outPoint_Y)); outPoint.Y = object_outPoint_Y_; auto method_result = unwrap()->TryTransform(inPoint, &outPoint); auto result = context.CreateBoolean(method_result); auto out_1 = context.CreateObject(); auto outPoint_X_ = context.CreateNumber(static_cast<double>(outPoint.X)); out_1.SetProperty("X", outPoint_X_); auto outPoint_Y_ = context.CreateNumber(static_cast<double>(outPoint.Y)); out_1.SetProperty("Y", outPoint_Y_); _1 = out_1; return result; } // Catch-all if no arg count matches! TITANIUM_LOG_DEBUG("No method signature matched GeneralTransform::TryTransform with # of args: ", arguments.size()); return context.CreateUndefined(); }