Emails js_to_Emails(const JSObject& object)
		{	
			std::vector<std::string> home;
			if (object.HasProperty("home")) {
				const auto js_home_object = static_cast<JSObject>(object.GetProperty("home"));
				if (js_home_object.IsArray()) {
					home = static_cast<std::vector<std::string>>(static_cast<JSArray>(js_home_object));
				}
			}
			std::vector<std::string> work;
			if (object.HasProperty("work")) {
				const auto js_work_object = static_cast<JSObject>(object.GetProperty("work"));
				if (js_work_object.IsArray()) {
					work = static_cast<std::vector<std::string>>(static_cast<JSArray>(js_work_object));
				}
			}
			std::vector<std::string> other;
			if (object.HasProperty("other")) {
				const auto js_other_object = static_cast<JSObject>(object.GetProperty("other"));
				if (js_other_object.IsArray()) {
					other = static_cast<std::vector<std::string>>(static_cast<JSArray>(js_other_object));
				}
			}

			Emails emails {
				home,
				work,
				other
			};
			
			return emails;
		};
		GradientColorRef js_to_GradientColorRef(const JSObject& object)
		{
			GradientColorRef colorRef;
			if (object.HasProperty("color")) {
				colorRef.color = static_cast<std::string>(object.GetProperty("color"));
			}
			if (object.HasProperty("offset")) {
				colorRef.offset = static_cast<double>(object.GetProperty("offset"));
			}
			return colorRef;
		}
		MapRegionTypev2 js_to_MapRegionTypev2(const JSObject& object)
		{
			MapRegionTypev2 value { 0, 0, 0, 0, 0, 0, 0 };
			if (object.HasProperty("bearing")) {
				value.bearing = static_cast<double>(object.GetProperty("bearing"));
			}
			if (object.HasProperty("latitude")) {
				value.latitude = static_cast<double>(object.GetProperty("latitude"));
			}
			if (object.HasProperty("latitudeDelta")) {
				value.latitudeDelta = static_cast<double>(object.GetProperty("latitudeDelta"));
			}
			if (object.HasProperty("longitude")) {
				value.longitude = static_cast<double>(object.GetProperty("longitude"));
			}
			if (object.HasProperty("longitudeDelta")) {
				value.longitudeDelta = static_cast<double>(object.GetProperty("longitudeDelta"));
			}
			if (object.HasProperty("tilt")) {
				value.tilt = static_cast<double>(object.GetProperty("tilt"));
			}
			if (object.HasProperty("zoom")) {
				value.zoom = static_cast<uint32_t>(object.GetProperty("zoom"));
			}
			return value;
		};
		CameraMediaItemType js_to_CameraMediaItemType(const JSObject& object)
		{
			CameraMediaItemType config;

			config.code = static_cast<std::int32_t>(object.GetProperty("code"));
			if (object.HasProperty("cropRect")) {
				config.cropRect = Titanium::UI::js_to_Dimension(static_cast<JSObject>(object.GetProperty("cropRect")));
			}
			if (object.HasProperty("error")) {
				config.error = static_cast<std::string>(object.GetProperty("error"));
			}
			const auto js_media = object.GetProperty("media");
			ENSURE_MODULE_OBJECT(js_media, media, Titanium::Blob);
			config.media = media;
			config.mediaType = static_cast<MediaType>(static_cast<std::uint32_t>(object.GetProperty("mediaType")));
			config.success = static_cast<bool>(object.GetProperty("success"));
			return config;
		};
Example #5
0
	void JSClass::ConstructorInitializeCallback(const JSContext& js_context, JSObject& this_object) const {
		if (!this_object.HasProperty("prototype")) {
			auto js_object = js_context.CreateObject();
			this_object.SetProperty("prototype", js_object);
		}

		auto proto_object = static_cast<JSObject>(this_object.GetProperty("prototype"));
		for (const auto v : prototype_functions_map__) {
			//
			// NOTE: On HAL, there's no difference between object "static" property and prototype property
			// so add it to both
			//
			this_object.SetProperty(v.first, JSObjectMakeFunctionWithCallback(js_context, v.first, v.second));
			proto_object.SetProperty(v.first, JSObjectMakeFunctionWithCallback(js_context, v.first, v.second));
		}
	}
		Gradient js_to_Gradient(const JSObject& object)
		{
			Gradient gradient;
			if (object.HasProperty("backfillEnd")) {
				gradient.backfillEnd = static_cast<bool>(object.GetProperty("backfillEnd"));
			}
			if (object.HasProperty("backfillStart")) {
				gradient.backfillStart = static_cast<bool>(object.GetProperty("backfillStart"));
			}
			if (object.HasProperty("endRadius")) {
				gradient.endRadius = static_cast<double>(object.GetProperty("endRadius"));
			}
			if (object.HasProperty("startRadius")) {
				gradient.startRadius = static_cast<double>(object.GetProperty("startRadius"));
			}
			if (object.HasProperty("endPoint")) {
				auto end_property = object.GetProperty("endPoint");
				if (end_property.IsObject()) {
					gradient.endPoint = js_to_Point(static_cast<JSObject>(end_property));
				} else {
					TITANIUM_LOG_WARN("js_to_Gradient: Invalid endPoint");
				}
			}
			if (object.HasProperty("startPoint")) {
				auto start_property = object.GetProperty("startPoint");
				if (start_property.IsObject()) {
					gradient.startPoint = js_to_Point(static_cast<JSObject>(start_property));
				} else {
					TITANIUM_LOG_WARN("js_to_Gradient: Invalid startPoint");
				}
			}
			if (object.HasProperty("type")) {
				auto type_property = object.GetProperty("type");
				if (type_property.IsNumber()) {
					gradient.type = Constants::to_GRADIENT_TYPE(static_cast<std::underlying_type<GRADIENT_TYPE>::type>(type_property));
				} else {
					gradient.type = Constants::to_GRADIENT_TYPE(static_cast<std::string>(type_property));
				}
			}
			if (object.HasProperty("color")) {
				auto color_property = object.GetProperty("color");
				if (color_property.IsString()) {
					gradient.color.push_back(js_to_GradientColorRef(static_cast<std::string>(color_property)));
				} else if (color_property.IsObject()) {
					auto js_color = static_cast<JSObject>(color_property);
					if (js_color.IsArray()) {
						const auto length = static_cast<uint32_t>(js_color.GetProperty("length"));
						for (uint32_t i = 0; i < length; i++) {
							auto item = js_color.GetProperty(i);
							TITANIUM_ASSERT(item.IsObject());
							if (item.IsObject()) {
								gradient.color.push_back(js_to_GradientColorRef(static_cast<JSObject>(item)));
							} else {
								TITANIUM_LOG_WARN("js_to_Gradient: Invalid GradientColorRef");
							}
						}
					} else {
						gradient.color.push_back(js_to_GradientColorRef(static_cast<std::string>(color_property)));
					}
				}

			}
			return gradient;
		};
TEST_F(FileTests, logging)
{
	JSContext js_context = js_context_group.CreateContext(JSExport<Titanium::GlobalObject>::Class());
	auto global_object = js_context.get_global_object();

	XCTAssertFalse(global_object.HasProperty("Titanium"));
	auto Titanium = js_context.CreateObject();
	global_object.SetProperty("Titanium", Titanium, {JSPropertyAttribute::ReadOnly, JSPropertyAttribute::DontDelete});
	XCTAssertTrue(global_object.HasProperty("Titanium"));

	// Make the alias "Ti" for the "Titanium" property.
	XCTAssertFalse(global_object.HasProperty("Ti"));
	global_object.SetProperty("Ti", Titanium, {JSPropertyAttribute::ReadOnly, JSPropertyAttribute::DontDelete});
	XCTAssertTrue(global_object.HasProperty("Ti"));

	XCTAssertFalse(Titanium.HasProperty("Filesystem"));
	auto Filesystem = js_context.CreateObject(JSExport<Titanium::FilesystemModule>::Class());
	Titanium.SetProperty("Filesystem", Filesystem, {JSPropertyAttribute::ReadOnly, JSPropertyAttribute::DontDelete});
	XCTAssertTrue(Titanium.HasProperty("Filesystem"));

	XCTAssertFalse(Filesystem.HasProperty("File"));
	auto File = js_context.CreateObject(JSExport<NativeFileExample>::Class());
	Filesystem.SetProperty("File", File, {JSPropertyAttribute::ReadOnly, JSPropertyAttribute::DontDelete});
	XCTAssertTrue(Filesystem.HasProperty("File"));

	XCTAssertTrue(Filesystem.HasProperty("createTempDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("createTempFile"));
	XCTAssertTrue(Filesystem.HasProperty("getFile"));
	XCTAssertTrue(Filesystem.HasProperty("isExternalStoragePresent"));
	XCTAssertTrue(Filesystem.HasProperty("openStream"));

	XCTAssertTrue(Filesystem.HasProperty("separator"));
	XCTAssertTrue(Filesystem.HasProperty("applicationCacheDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("applicationDataDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("applicationDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("applicationSupportDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("externalStorageDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("lineEnding"));
	XCTAssertTrue(Filesystem.HasProperty("resourcesDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("tempDirectory"));

	XCTAssertTrue(Filesystem.HasProperty("getSeparator"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationCacheDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationDataDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationSupportDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getExternalStorageDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getLineEnding"));
	XCTAssertTrue(Filesystem.HasProperty("getResourcesDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getTempDirectory"));

	auto File_ptr = File.GetPrivate<NativeFileExample>();
	XCTAssertNotEqual(nullptr, File_ptr);

	JSValue result = js_context.CreateNull();
	XCTAssertNoThrow(result = File.CallAsConstructor(js_context.CreateNull()));
	XCTAssertTrue(result.IsObject());

	JSObject file = static_cast<JSObject>(result);

	XCTAssertTrue(file.HasProperty("executable"));
	XCTAssertTrue(file.HasProperty("hidden"));
	XCTAssertTrue(file.HasProperty("name"));
	XCTAssertTrue(file.HasProperty("nativePath"));
	XCTAssertTrue(file.HasProperty("parent"));
	XCTAssertTrue(file.HasProperty("readonly"));
	XCTAssertTrue(file.HasProperty("remoteBackup"));
	XCTAssertTrue(file.HasProperty("size"));
	XCTAssertTrue(file.HasProperty("symbolicLink"));
	XCTAssertTrue(file.HasProperty("writable"));
	XCTAssertTrue(file.HasProperty("getExecutable"));
	XCTAssertTrue(file.HasProperty("getHidden"));
	XCTAssertTrue(file.HasProperty("getName"));
	XCTAssertTrue(file.HasProperty("getNativePath"));
	XCTAssertTrue(file.HasProperty("getParent"));
	XCTAssertTrue(file.HasProperty("getReadonly"));
	XCTAssertTrue(file.HasProperty("getRemoteBackup"));
	XCTAssertTrue(file.HasProperty("getSize"));
	XCTAssertTrue(file.HasProperty("getSymbolicLink"));
	XCTAssertTrue(file.HasProperty("getWritable"));
	XCTAssertTrue(file.HasProperty("append"));
	XCTAssertTrue(file.HasProperty("copy"));
	XCTAssertTrue(file.HasProperty("createDirectory"));
	XCTAssertTrue(file.HasProperty("createFile"));
	XCTAssertTrue(file.HasProperty("createTimestamp"));
	XCTAssertTrue(file.HasProperty("deleteDirectory"));
	XCTAssertTrue(file.HasProperty("deleteFile"));
	XCTAssertTrue(file.HasProperty("exists"));
	XCTAssertTrue(file.HasProperty("extension"));
	XCTAssertTrue(file.HasProperty("getDirectoryListing"));
	XCTAssertTrue(file.HasProperty("isDirectory"));
	XCTAssertTrue(file.HasProperty("isFile"));
	XCTAssertTrue(file.HasProperty("modificationTimestamp"));
	XCTAssertTrue(file.HasProperty("move"));
	XCTAssertTrue(file.HasProperty("open"));
	XCTAssertTrue(file.HasProperty("read"));
	XCTAssertTrue(file.HasProperty("rename"));
	XCTAssertTrue(file.HasProperty("resolve"));
	XCTAssertTrue(file.HasProperty("spaceAvailable"));
	XCTAssertTrue(file.HasProperty("write"));
}
TEST_F(FileTests, logging)
{
	JSContext js_context = js_context_group.CreateContext(JSExport<Titanium::GlobalObject>::Class());
	auto global_object = js_context.get_global_object();

	auto Filesystem = js_context.CreateObject(JSExport<Titanium::FilesystemModule>::Class());
	auto File = js_context.CreateObject(JSExport<NativeFileExample>::Class());

	Filesystem.SetProperty("File", File);
	global_object.SetProperty("Filesystem", Filesystem);

	XCTAssertTrue(Filesystem.HasProperty("createTempDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("createTempFile"));
	XCTAssertTrue(Filesystem.HasProperty("getFile"));
	XCTAssertTrue(Filesystem.HasProperty("isExternalStoragePresent"));
	XCTAssertTrue(Filesystem.HasProperty("openStream"));

	XCTAssertTrue(Filesystem.HasProperty("separator"));
	XCTAssertTrue(Filesystem.HasProperty("applicationCacheDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("applicationDataDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("applicationDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("applicationSupportDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("externalStorageDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("lineEnding"));
	XCTAssertTrue(Filesystem.HasProperty("resourcesDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("tempDirectory"));

	XCTAssertTrue(Filesystem.HasProperty("getSeparator"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationCacheDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationDataDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getApplicationSupportDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getExternalStorageDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getLineEnding"));
	XCTAssertTrue(Filesystem.HasProperty("getResourcesDirectory"));
	XCTAssertTrue(Filesystem.HasProperty("getTempDirectory"));

	auto json_result = js_context.JSEvaluateScript("JSON.stringify(Filesystem);");
	XCTAssertTrue(static_cast<std::string>(json_result).find("\"separator\":") != std::string::npos);

	auto File_ptr = File.GetPrivate<NativeFileExample>();
	XCTAssertNotEqual(nullptr, File_ptr);

	JSValue result = js_context.CreateNull();
	XCTAssertNoThrow(result = File.CallAsConstructor(js_context.CreateNull()));
	XCTAssertTrue(result.IsObject());

	JSObject file = static_cast<JSObject>(result);

	XCTAssertTrue(file.HasProperty("executable"));
	XCTAssertTrue(file.HasProperty("hidden"));
	XCTAssertTrue(file.HasProperty("name"));
	XCTAssertTrue(file.HasProperty("nativePath"));
	XCTAssertTrue(file.HasProperty("parent"));
	XCTAssertTrue(file.HasProperty("readonly"));
	XCTAssertTrue(file.HasProperty("remoteBackup"));
	XCTAssertTrue(file.HasProperty("size"));
	XCTAssertTrue(file.HasProperty("symbolicLink"));
	XCTAssertTrue(file.HasProperty("writable"));
	XCTAssertTrue(file.HasProperty("getExecutable"));
	XCTAssertTrue(file.HasProperty("getHidden"));
	XCTAssertTrue(file.HasProperty("getName"));
	XCTAssertTrue(file.HasProperty("getNativePath"));
	XCTAssertTrue(file.HasProperty("getParent"));
	XCTAssertTrue(file.HasProperty("getReadonly"));
	XCTAssertTrue(file.HasProperty("getRemoteBackup"));
	XCTAssertTrue(file.HasProperty("getSize"));
	XCTAssertTrue(file.HasProperty("getSymbolicLink"));
	XCTAssertTrue(file.HasProperty("getWritable"));
	XCTAssertTrue(file.HasProperty("append"));
	XCTAssertTrue(file.HasProperty("copy"));
	XCTAssertTrue(file.HasProperty("createDirectory"));
	XCTAssertTrue(file.HasProperty("createFile"));
	XCTAssertTrue(file.HasProperty("createTimestamp"));
	XCTAssertTrue(file.HasProperty("deleteDirectory"));
	XCTAssertTrue(file.HasProperty("deleteFile"));
	XCTAssertTrue(file.HasProperty("exists"));
	XCTAssertTrue(file.HasProperty("extension"));
	XCTAssertTrue(file.HasProperty("getDirectoryListing"));
	XCTAssertTrue(file.HasProperty("isDirectory"));
	XCTAssertTrue(file.HasProperty("isFile"));
	XCTAssertTrue(file.HasProperty("modificationTimestamp"));
	XCTAssertTrue(file.HasProperty("move"));
	XCTAssertTrue(file.HasProperty("open"));
	XCTAssertTrue(file.HasProperty("read"));
	XCTAssertTrue(file.HasProperty("rename"));
	XCTAssertTrue(file.HasProperty("resolve"));
	XCTAssertTrue(file.HasProperty("spaceAvailable"));
	XCTAssertTrue(file.HasProperty("write"));

	global_object.SetProperty("file", file);
	json_result = js_context.JSEvaluateScript("JSON.stringify(file);");
	XCTAssertTrue(static_cast<std::string>(json_result).find("\"symbolicLink\":") != std::string::npos);
}
		ShowContactsParams js_to_ShowContactsParams(const JSObject& object)
		{
			auto animated = true;
			if (object.HasProperty("animated")) {
				animated = static_cast<bool>(object.GetProperty("animated"));
			}
			
			std::vector<std::string> fields;
			if (object.HasProperty("fields")) {
				const auto js_fields_object = static_cast<JSObject>(object.GetProperty("fields"));
				if (js_fields_object.IsArray()) {
					const auto js_fields = static_cast<std::vector<JSValue>>(static_cast<JSArray>(js_fields_object));
					for (const auto f : js_fields) {
						fields.push_back(static_cast<std::string>(f));
					}
				}
			}

			const auto cancel_property = object.GetProperty("cancel");
			const auto oncancel = [cancel_property]() {
				if (cancel_property.IsObject()) {
					auto func = static_cast<JSObject>(cancel_property);
					if (func.IsFunction()) {
						const std::vector<JSValue> args = {};
						func(args, func);
					}
				}
			};

			const auto selectedPerson_property = object.GetProperty("selectedPerson");
			const auto onselectedPerson = [selectedPerson_property](const std::shared_ptr<Person>& e) {
				if (selectedPerson_property.IsObject()) {
					auto func = static_cast<JSObject>(selectedPerson_property);
					if (func.IsFunction()) {
						auto obj = func.get_context().CreateObject();
						obj.SetProperty("person", e->get_object());
						const std::vector<JSValue> args = {
							static_cast<JSValue>(obj)
						};
						func(args, func);
					}
				}
			};

			const auto selectedProperty_property = object.GetProperty("selectedProperty");
			const auto onselectedProperty = [selectedProperty_property](const std::shared_ptr<Person>& e) {
				if (selectedProperty_property.IsObject()) {
					auto func = static_cast<JSObject>(selectedProperty_property);
					if (func.IsFunction()) {
						auto obj = func.get_context().CreateObject();
						obj.SetProperty("person", e->get_object());
						const std::vector<JSValue> args = {
							static_cast<JSValue>(obj)
						};
						func(args, func);
					}
				}
			};

			ShowContactsParamsCallbacks callbacks {
				cancel_property,
				selectedPerson_property,
				selectedProperty_property,
				oncancel,
				onselectedPerson,
				onselectedProperty
			};

			ShowContactsParams config {
				animated,
				fields,
				callbacks
			};
			
			return config;
		};