private JToken Visit(JavaScriptValue value) { switch (value.ValueType) { case JavaScriptValueType.Array: return VisitArray(value); case JavaScriptValueType.Boolean: return VisitBoolean(value); case JavaScriptValueType.Error: return VisitError(value); case JavaScriptValueType.Null: return VisitNull(value); case JavaScriptValueType.Number: return VisitNumber(value); case JavaScriptValueType.Object: return VisitObject(value); case JavaScriptValueType.String: return VisitString(value); case JavaScriptValueType.Undefined: return VisitUndefined(value); case JavaScriptValueType.Function: default: throw new NotSupportedException(); } }
private JToken VisitNumber(JavaScriptValue value) { var number = value.ToDouble(); return number % 1 == 0 ? new JValue((long)number) : new JValue(number); }
private JToken VisitError(JavaScriptValue value) { return new JObject { { "message", Visit(value.GetProperty(JavaScriptPropertyId.FromString("message"))) }, { "description", Visit(value.GetProperty(JavaScriptPropertyId.FromString("description"))) }, { "stack", Visit(value.GetProperty(JavaScriptPropertyId.FromString("stack"))) }, }; }
private JToken VisitArray(JavaScriptValue value) { var array = new JArray(); var propertyId = JavaScriptPropertyId.FromString("length"); var length = (int)value.GetProperty(propertyId).ToDouble(); for (var i = 0; i < length; ++i) { var index = JavaScriptValue.FromInt32(i); var element = value.GetIndexedProperty(index); array.Add(Visit(element)); } return array; }
private JToken VisitUndefined(JavaScriptValue value) { return s_undefined; }
private JToken VisitString(JavaScriptValue value) { return JValue.CreateString(value.ToString()); }
private JavaScriptErrorCode FetchImportedModuleFromScript(JavaScriptSourceContext sourceContext, JavaScriptValue source, out JavaScriptModuleRecord result) { // Debug.Log("FetchImportedModuleFromScriptDelegate start"); result = new JavaScriptModuleRecord(); return(JavaScriptErrorCode.NoError); }
/// <summary> /// Initializes a new instance of the <see cref="JavaScriptScriptException"/> class. /// </summary> /// <param name="code">The error code returned.</param> /// <param name="error">The JavaScript error object.</param> public JavaScriptScriptException(JavaScriptErrorCode code, JavaScriptValue error) : this(code, error, "JavaScript Exception") { }
public static void Register(JavaScriptContext context) { JavaScriptValue NetworkTransportPrototype; JavaScriptValue NetworkTransportConstructor = Bridge.CreateConstructor( typeof(UnityEngine.Networking.NetworkTransport), (args) => { throw new NotImplementedException(); }, out NetworkTransportPrototype ); JavaScriptValue .GlobalObject .GetProperty("UnityEngine") .GetProperty("Networking") .SetProperty("NetworkTransport", NetworkTransportConstructor); // Static Fields // Static Property Accessors Bridge.DefineGetter( NetworkTransportConstructor, "IsStarted", (args) => JavaScriptValue.FromBoolean(UnityEngine.Networking.NetworkTransport.IsStarted) ); // Static Methods NetworkTransportConstructor.SetProperty( "Init", Bridge.CreateFunction( "Init", (args) => UnityEngine.Networking.NetworkTransport.Init() ) ); NetworkTransportConstructor.SetProperty( "Shutdown", Bridge.CreateFunction( "Shutdown", (args) => UnityEngine.Networking.NetworkTransport.Shutdown() ) ); NetworkTransportConstructor.SetProperty( "AddSceneId", Bridge.CreateFunction( "AddSceneId", (args) => UnityEngine.Networking.NetworkTransport.AddSceneId(args[1].ToInt32()) ) ); NetworkTransportConstructor.SetProperty( "GetNextSceneId", Bridge.CreateFunction( "GetNextSceneId", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetNextSceneId()) ) ); /* * NetworkTransport ConnectAsNetworkHost * parameter error is out */ /* * NetworkTransport DisconnectNetworkHost * parameter error is out */ /* * NetworkTransport ReceiveRelayEventFromHost * parameter error is out */ /* * NetworkTransport ConnectToNetworkPeer * parameter error is out */ /* * NetworkTransport ConnectToNetworkPeer * parameter error is out */ /* * NetworkTransport GetIncomingMessageQueueSize * parameter error is out */ /* * NetworkTransport GetOutgoingMessageQueueSize * parameter error is out */ /* * NetworkTransport GetCurrentRTT * parameter error is out */ /* * NetworkTransport GetIncomingPacketLossCount * parameter error is out */ /* * NetworkTransport GetIncomingPacketCount * parameter error is out */ /* * NetworkTransport GetOutgoingPacketNetworkLossPercent * parameter error is out */ /* * NetworkTransport GetOutgoingPacketOverflowLossPercent * parameter error is out */ /* * NetworkTransport GetMaxAllowedBandwidth * parameter error is out */ /* * NetworkTransport GetAckBufferCount * parameter error is out */ NetworkTransportConstructor.SetProperty( "GetIncomingPacketDropCountForAllHosts", Bridge.CreateFunction( "GetIncomingPacketDropCountForAllHosts", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetIncomingPacketDropCountForAllHosts()) ) ); NetworkTransportConstructor.SetProperty( "GetIncomingPacketCountForAllHosts", Bridge.CreateFunction( "GetIncomingPacketCountForAllHosts", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetIncomingPacketCountForAllHosts()) ) ); NetworkTransportConstructor.SetProperty( "GetOutgoingPacketCount", Bridge.CreateFunction( "GetOutgoingPacketCount", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetOutgoingPacketCount()) ) ); /* * NetworkTransport GetOutgoingPacketCountForHost * parameter error is out */ /* * NetworkTransport GetOutgoingPacketCountForConnection * parameter error is out */ NetworkTransportConstructor.SetProperty( "GetOutgoingMessageCount", Bridge.CreateFunction( "GetOutgoingMessageCount", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetOutgoingMessageCount()) ) ); /* * NetworkTransport GetOutgoingMessageCountForHost * parameter error is out */ /* * NetworkTransport GetOutgoingMessageCountForConnection * parameter error is out */ NetworkTransportConstructor.SetProperty( "GetOutgoingUserBytesCount", Bridge.CreateFunction( "GetOutgoingUserBytesCount", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetOutgoingUserBytesCount()) ) ); /* * NetworkTransport GetOutgoingUserBytesCountForHost * parameter error is out */ /* * NetworkTransport GetOutgoingUserBytesCountForConnection * parameter error is out */ NetworkTransportConstructor.SetProperty( "GetOutgoingSystemBytesCount", Bridge.CreateFunction( "GetOutgoingSystemBytesCount", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetOutgoingSystemBytesCount()) ) ); /* * NetworkTransport GetOutgoingSystemBytesCountForHost * parameter error is out */ /* * NetworkTransport GetOutgoingSystemBytesCountForConnection * parameter error is out */ NetworkTransportConstructor.SetProperty( "GetOutgoingFullBytesCount", Bridge.CreateFunction( "GetOutgoingFullBytesCount", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetOutgoingFullBytesCount()) ) ); /* * NetworkTransport GetOutgoingFullBytesCountForHost * parameter error is out */ /* * NetworkTransport GetOutgoingFullBytesCountForConnection * parameter error is out */ /* * NetworkTransport GetConnectionInfo * parameter address is out */ /* * NetworkTransport GetConnectionInfo * parameter port is out */ NetworkTransportConstructor.SetProperty( "GetNetworkTimestamp", Bridge.CreateFunction( "GetNetworkTimestamp", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetNetworkTimestamp()) ) ); /* * NetworkTransport GetRemoteDelayTimeMS * parameter error is out */ /* * NetworkTransport StartSendMulticast * parameter error is out */ /* * NetworkTransport SendMulticast * parameter error is out */ /* * NetworkTransport FinishSendMulticast * parameter error is out */ NetworkTransportConstructor.SetProperty( "AddWebsocketHost", Bridge.CreateFunction( "AddWebsocketHost", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddWebsocketHost(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32())) ) ); NetworkTransportConstructor.SetProperty( "AddWebsocketHost", Bridge.CreateFunction( "AddWebsocketHost", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddWebsocketHost(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32(), args[3].ToString())) ) ); NetworkTransportConstructor.SetProperty( "AddHost", Bridge.CreateFunction( "AddHost", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddHost(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32())) ) ); NetworkTransportConstructor.SetProperty( "AddHost", Bridge.CreateFunction( "AddHost", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddHost(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]))) ) ); NetworkTransportConstructor.SetProperty( "AddHost", Bridge.CreateFunction( "AddHost", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddHost(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32(), args[3].ToString())) ) ); NetworkTransportConstructor.SetProperty( "AddHostWithSimulator", Bridge.CreateFunction( "AddHostWithSimulator", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddHostWithSimulator(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32(), args[3].ToInt32(), args[4].ToInt32())) ) ); NetworkTransportConstructor.SetProperty( "AddHostWithSimulator", Bridge.CreateFunction( "AddHostWithSimulator", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddHostWithSimulator(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32(), args[3].ToInt32())) ) ); NetworkTransportConstructor.SetProperty( "AddHostWithSimulator", Bridge.CreateFunction( "AddHostWithSimulator", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.AddHostWithSimulator(Bridge.GetExternal <UnityEngine.Networking.HostTopology>(args[1]), args[2].ToInt32(), args[3].ToInt32(), args[4].ToInt32(), args[5].ToString())) ) ); NetworkTransportConstructor.SetProperty( "RemoveHost", Bridge.CreateFunction( "RemoveHost", (args) => JavaScriptValue.FromBoolean(UnityEngine.Networking.NetworkTransport.RemoveHost(args[1].ToInt32())) ) ); /* * NetworkTransport Connect * parameter error is out */ /* * NetworkTransport ConnectWithSimulator * parameter error is out */ /* * NetworkTransport Disconnect * parameter error is out */ /* * NetworkTransport Send * parameter error is out */ /* * NetworkTransport QueueMessageForSending * parameter error is out */ /* * NetworkTransport SendQueuedMessages * parameter error is out */ /* * NetworkTransport Receive * parameter hostId is out */ /* * NetworkTransport ReceiveFromHost * parameter connectionId is out */ NetworkTransportConstructor.SetProperty( "SetPacketStat", Bridge.CreateFunction( "SetPacketStat", (args) => UnityEngine.Networking.NetworkTransport.SetPacketStat(args[1].ToInt32(), args[2].ToInt32(), args[3].ToInt32(), args[4].ToInt32()) ) ); /* * NetworkTransport StartBroadcastDiscovery * parameter error is out */ NetworkTransportConstructor.SetProperty( "StopBroadcastDiscovery", Bridge.CreateFunction( "StopBroadcastDiscovery", (args) => UnityEngine.Networking.NetworkTransport.StopBroadcastDiscovery() ) ); NetworkTransportConstructor.SetProperty( "IsBroadcastDiscoveryRunning", Bridge.CreateFunction( "IsBroadcastDiscoveryRunning", (args) => JavaScriptValue.FromBoolean(UnityEngine.Networking.NetworkTransport.IsBroadcastDiscoveryRunning()) ) ); /* * NetworkTransport SetBroadcastCredentials * parameter error is out */ /* * NetworkTransport GetBroadcastConnectionInfo * parameter port is out */ /* * NetworkTransport GetBroadcastConnectionInfo * parameter address is out */ /* * NetworkTransport GetBroadcastConnectionMessage * parameter receivedSize is out */ /* * NetworkTransport ConnectEndPoint * parameter error is out */ NetworkTransportConstructor.SetProperty( "Init", Bridge.CreateFunction( "Init", (args) => UnityEngine.Networking.NetworkTransport.Init(Bridge.GetExternal <UnityEngine.Networking.GlobalConfig>(args[1])) ) ); /* * NetworkTransport NotifyWhenConnectionReadyForSend * parameter error is out */ NetworkTransportConstructor.SetProperty( "GetHostPort", Bridge.CreateFunction( "GetHostPort", (args) => JavaScriptValue.FromInt32(UnityEngine.Networking.NetworkTransport.GetHostPort(args[1].ToInt32())) ) ); // Instance Fields // Instance Property Accessors // Instance Methods }
public static void Register(JavaScriptContext context) { JavaScriptValue ColorPrototype; JavaScriptValue ColorConstructor = Bridge.CreateConstructor( typeof(UnityEngine.Color), (args) => { throw new System.NotImplementedException(); }, out ColorPrototype ); JavaScriptValue .GlobalObject .GetProperty("UnityEngine") .SetProperty("Color", ColorConstructor); // Static Fields // Static Property Accessors Bridge.DefineGetter( ColorConstructor, "red", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.red) ); Bridge.DefineGetter( ColorConstructor, "green", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.green) ); Bridge.DefineGetter( ColorConstructor, "blue", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.blue) ); Bridge.DefineGetter( ColorConstructor, "white", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.white) ); Bridge.DefineGetter( ColorConstructor, "black", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.black) ); Bridge.DefineGetter( ColorConstructor, "yellow", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.yellow) ); Bridge.DefineGetter( ColorConstructor, "cyan", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.cyan) ); Bridge.DefineGetter( ColorConstructor, "magenta", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.magenta) ); Bridge.DefineGetter( ColorConstructor, "gray", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.gray) ); Bridge.DefineGetter( ColorConstructor, "grey", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.grey) ); Bridge.DefineGetter( ColorConstructor, "clear", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.clear) ); // Static Methods ColorConstructor.SetProperty( "Lerp", Bridge.CreateFunction( "Lerp", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.Lerp(Bridge.GetBoxedExternal <UnityEngine.Color>(args[1]).wrapped, Bridge.GetBoxedExternal <UnityEngine.Color>(args[2]).wrapped, (float)args[3].ToDouble())) ) ); ColorConstructor.SetProperty( "LerpUnclamped", Bridge.CreateFunction( "LerpUnclamped", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.LerpUnclamped(Bridge.GetBoxedExternal <UnityEngine.Color>(args[1]).wrapped, Bridge.GetBoxedExternal <UnityEngine.Color>(args[2]).wrapped, (float)args[3].ToDouble())) ) ); /* * Color RGBToHSV * parameter H is out */ ColorConstructor.SetProperty( "HSVToRGB", Bridge.CreateFunction( "HSVToRGB", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.HSVToRGB((float)args[1].ToDouble(), (float)args[2].ToDouble(), (float)args[3].ToDouble())) ) ); ColorConstructor.SetProperty( "HSVToRGB", Bridge.CreateFunction( "HSVToRGB", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.Color.HSVToRGB((float)args[1].ToDouble(), (float)args[2].ToDouble(), (float)args[3].ToDouble(), args[4].ToBoolean())) ) ); // Instance Fields Bridge.DefineGetterSetter( ColorPrototype, "r", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromDouble(o.wrapped.r)), Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => { o.wrapped.r = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( ColorPrototype, "g", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromDouble(o.wrapped.g)), Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => { o.wrapped.g = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( ColorPrototype, "b", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromDouble(o.wrapped.b)), Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => { o.wrapped.b = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( ColorPrototype, "a", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromDouble(o.wrapped.a)), Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => { o.wrapped.a = (float)args[1].ToDouble(); }) ); // Instance Property Accessors Bridge.DefineGetter( ColorPrototype, "grayscale", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromDouble(o.wrapped.grayscale)) ); Bridge.DefineGetter( ColorPrototype, "linear", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => Bridge.CreateExternalWithPrototype(o.wrapped.linear)) ); Bridge.DefineGetter( ColorPrototype, "gamma", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => Bridge.CreateExternalWithPrototype(o.wrapped.gamma)) ); Bridge.DefineGetter( ColorPrototype, "maxColorComponent", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromDouble(o.wrapped.maxColorComponent)) ); // Instance Methods ColorPrototype.SetProperty( "toString", Bridge.CreateFunction( "ToString", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => { if (args.Length == 1) { return(JavaScriptValue.FromString(o.wrapped.ToString())); } else { return(JavaScriptValue.FromString(o.wrapped.ToString(args[1].ToString()))); } }) ) ); ColorPrototype.SetProperty( "GetHashCode", Bridge.CreateFunction( "GetHashCode", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromInt32(o.wrapped.GetHashCode())) ) ); // ColorPrototype.SetProperty( // "Equals", // Bridge.CreateFunction( // "Equals", // Bridge.WithBoxedExternal<UnityEngine.Color>((o, args) => JavaScriptValue.FromBoolean(o.wrapped.Equals(Bridge.GetExternal<System.Object>(args[1])))) // ) // ); ColorPrototype.SetProperty( "Equals", Bridge.CreateFunction( "Equals", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => JavaScriptValue.FromBoolean(o.wrapped.Equals(Bridge.GetBoxedExternal <UnityEngine.Color>(args[1]).wrapped))) ) ); ColorPrototype.SetProperty( "ToColor32", Bridge.CreateFunction( "ToColor32", Bridge.WithBoxedExternal <UnityEngine.Color>((o, args) => { UnityEngine.Color32 asColor32 = o.wrapped; return(Bridge.CreateExternalWithPrototype(asColor32)); }) ) ); }
private JToken ConvertJson(JavaScriptValue value) { return(JavaScriptValueToJTokenConverter.Convert(value)); }
private JavaScriptContext CreateHostContext(JavaScriptRuntime runtime, HttpListenerRequest request) { // Create the context. Note that if we had wanted to start debugging from the very // beginning, we would have called JsStartDebugging right after context is created. JavaScriptContext context = runtime.CreateContext(); // Now set the execution context as being the current one on this thread. using (new JavaScriptContext.Scope(context)) { // Create the host object the script will use. JavaScriptValue hostObject = JavaScriptValue.CreateObject(); // Get the global object JavaScriptValue globalObject = JavaScriptValue.GlobalObject; // Get the name of the property ("host") that we're going to set on the global object. JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host"); // Set the property. globalObject.SetProperty(hostPropertyId, hostObject, true); // Now create the host callbacks that we're going to expose to the script. DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "readFile", readFileDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "writeFile", writeFileDelegate, IntPtr.Zero); // Create an object for request. JavaScriptValue requestParams = JavaScriptValue.CreateObject(); if (request.RawUrl != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("rawUrl"), JavaScriptValue.FromString(request.RawUrl), true); } if (request.UserAgent != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("userAgent"), JavaScriptValue.FromString(request.UserAgent), true); } if (request.UserHostName != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("userHostName"), JavaScriptValue.FromString(request.UserHostName), true); } if (request.UserHostAddress != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("userHostAddress"), JavaScriptValue.FromString(request.UserHostAddress), true); } if (request.ServiceName != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("serviceName"), JavaScriptValue.FromString(request.ServiceName), true); } if (request.HttpMethod != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("httpMethod"), JavaScriptValue.FromString(request.HttpMethod), true); } if (request.ContentType != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("contentType"), JavaScriptValue.FromString(request.ContentType), true); } if (request.ContentEncoding != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("contentEncoding"), JavaScriptValue.FromString(request.ContentEncoding.WebName), true); } requestParams.SetProperty(JavaScriptPropertyId.FromString("keepAlive"), JavaScriptValue.FromBoolean(request.KeepAlive), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isWebSocketRequest"), JavaScriptValue.FromBoolean(request.IsWebSocketRequest), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isSecureConnection"), JavaScriptValue.FromBoolean(request.IsSecureConnection), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isLocal"), JavaScriptValue.FromBoolean(request.IsLocal), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isAuthenticated"), JavaScriptValue.FromBoolean(request.IsAuthenticated), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("hasEntityBody"), JavaScriptValue.FromBoolean(request.HasEntityBody), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("contentLength64"), JavaScriptValue.FromDouble(request.ContentLength64), true); // need to call begingetclientcertificate //requestParams.SetProperty(JavaScriptPropertyId.FromString("clientCertificateError"), JavaScriptValue.FromInt32(request.ClientCertificateError), true); if (request.UrlReferrer != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("urlReferrer"), JavaScriptValue.FromString(request.UrlReferrer.ToString()), true); } if (request.RequestTraceIdentifier != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("requestTraceIdentifier"), JavaScriptValue.FromString(request.RequestTraceIdentifier.ToString()), true); } if (request.RemoteEndPoint != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("remoteEndPoint"), JavaScriptValue.FromString(request.RemoteEndPoint.ToString()), true); } if (request.ProtocolVersion != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("protocolVersion"), JavaScriptValue.FromString(request.ProtocolVersion.ToString()), true); } if (request.LocalEndPoint != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("localEndPoint"), JavaScriptValue.FromString(request.LocalEndPoint.ToString()), true); } if (request.UserLanguages != null) { JavaScriptValue userLanguages = JavaScriptValue.CreateArray((uint)request.UserLanguages.Length); for (int i = 0; i < request.UserLanguages.Length; i++) { userLanguages.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(request.UserLanguages[i])); } requestParams.SetProperty(JavaScriptPropertyId.FromString("userLanguages"), userLanguages, true); } if (request.AcceptTypes != null) { JavaScriptValue acceptTypes = JavaScriptValue.CreateArray((uint)request.AcceptTypes.Length); for (int i = 0; i < request.AcceptTypes.Length; i++) { acceptTypes.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(request.AcceptTypes[i])); } requestParams.SetProperty(JavaScriptPropertyId.FromString("acceptTypes"), acceptTypes, true); } if (request.QueryString != null) { JavaScriptValue queryString = JavaScriptValue.CreateArray((uint)request.QueryString.Count); for (int i = 0; i < request.QueryString.Count; i++) { JavaScriptValue qsItem = JavaScriptValue.CreateObject(); qsItem.SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(request.QueryString.GetKey(i) ?? string.Empty), false); qsItem.SetProperty(JavaScriptPropertyId.FromString("value"), JavaScriptValue.FromString(request.QueryString[i] ?? string.Empty), false); queryString.SetIndexedProperty(JavaScriptValue.FromInt32(i), qsItem); } requestParams.SetProperty(JavaScriptPropertyId.FromString("queryString"), queryString, true); } if (request.Headers != null) { JavaScriptValue headers = JavaScriptValue.CreateArray((uint)request.Headers.Count); for (int i = 0; i < request.Headers.Count; i++) { JavaScriptValue headerItem = JavaScriptValue.CreateObject(); headerItem.SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(request.Headers.GetKey(i) ?? string.Empty), false); headerItem.SetProperty(JavaScriptPropertyId.FromString("value"), JavaScriptValue.FromString(request.Headers[i] ?? string.Empty), false); headers.SetIndexedProperty(JavaScriptValue.FromInt32(i), headerItem); } requestParams.SetProperty(JavaScriptPropertyId.FromString("headers"), headers, true); } // #todo // Stream InputStream // CookieCollection Cookies hostObject.SetProperty(JavaScriptPropertyId.FromString("request"), requestParams, true); } return(context); }
private JavaScriptContext CreateHostWebsocketContext(JavaScriptRuntime runtime, string clientId, string text, Stream binaryStream, bool isStream) { // Create the context. Note that if we had wanted to start debugging from the very // beginning, we would have called JsStartDebugging right after context is created. JavaScriptContext context = runtime.CreateContext(); // Now set the execution context as being the current one on this thread. using (new JavaScriptContext.Scope(context)) { // Create the host object the script will use. JavaScriptValue hostObject = JavaScriptValue.CreateObject(); // Get the global object JavaScriptValue globalObject = JavaScriptValue.GlobalObject; // Get the name of the property ("host") that we're going to set on the global object. JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host"); // Set the property. globalObject.SetProperty(hostPropertyId, hostObject, true); // Now create the host callbacks that we're going to expose to the script. DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "readFile", readFileDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "writeFile", writeFileDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "websocketClients", getWebSocketClientIdDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "websocketPush", pushWebSocketMessageDelegate, IntPtr.Zero); JavaScriptValue requestParams = JavaScriptValue.CreateObject(); if (isStream) { requestParams.SetProperty(JavaScriptPropertyId.FromString("blob"), JavaScriptValue.FromString(text), true); // not implemented yet #todo } else { requestParams.SetProperty(JavaScriptPropertyId.FromString("text"), JavaScriptValue.FromString(text), true); } requestParams.SetProperty(JavaScriptPropertyId.FromString("clientId"), JavaScriptValue.FromString(clientId), true); hostObject.SetProperty(JavaScriptPropertyId.FromString("request"), requestParams, true); } return(context); }
// Add methods for windowExamples object in Formium.external object in JavaScript context. private void RegisterWindowStyleExampleObject() { var windowStyleDemo = JavaScriptValue.CreateObject(); windowStyleDemo.SetValue("openNativeStyleForm", JavaScriptValue.CreateFunction(args => { InvokeIfRequired(() => { if (nativeStyleForm == null || nativeStyleForm.IsDisposed) { nativeStyleForm = new NativeStyleForm(); nativeStyleForm.Show(); } else { if (!nativeStyleForm.Visible) { nativeStyleForm.Show(); } nativeStyleForm.Active(); } }); return(null); })); windowStyleDemo.SetValue("openKisokModeForm", JavaScriptValue.CreateFunction(args => { InvokeIfRequired(() => { if (kisokModeForm == null || kisokModeForm.IsDisposed) { kisokModeForm = new KisokModeForm(); kisokModeForm.Show(); } else { kisokModeForm.Close(); } }); return(null); })); windowStyleDemo.SetValue("openAcrylicStyleForm", JavaScriptValue.CreateFunction(args => { InvokeIfRequired(() => { if (acrylicStyleForm == null || acrylicStyleForm.IsDisposed) { acrylicStyleForm = new AcrylicStyleForm(); acrylicStyleForm.Show(HostWindow); } else { if (!acrylicStyleForm.Visible) { acrylicStyleForm.Show(); } acrylicStyleForm.Active(); } }); return(null); })); windowStyleDemo.SetValue("openLayeredStyleForm", JavaScriptValue.CreateFunction(args => { InvokeIfRequired(() => { var layeredStyleForm = new LayeredStyleForm(); layeredStyleForm.Show(HostWindow); }); return(null); })); RegisterExternalObjectValue("windowExamples", windowStyleDemo); }
// Example: Register JavaScript object in current frame. private void RegisterJavaScriptExampleObject() { var jsDemo = JavaScriptValue.CreateObject(); jsDemo.SetValue("executeJavaScriptWithoutRetval", JavaScriptValue.CreateFunction(args => { InvokeIfRequired(() => { ExecuteJavaScript("alert(1+1)"); }); return(null); })); jsDemo.SetValue("executeJavaScriptWithRetval", JavaScriptValue.CreateFunction(args => { InvokeIfRequired(async() => { var result = await EvaluateJavaScriptAsync("(function(){ return {a:1,b:'hello',c:(s)=>alert(s)}; })()"); if (result.Success) { var retval = result.ResultValue; MessageBox.Show($"a={retval.GetInt("a")} b={retval.GetString("b")}", "Value from JS"); await retval.GetValue("c").ExecuteFunctionAsync(GetMainFrame(), new JavaScriptValue[] { JavaScriptValue.CreateString("Hello from C#") }); } }); return(null); })); RegisterExternalObjectValue("jsExamples", jsDemo); }
public static JToken Convert(JavaScriptValue value) { return s_instance.Visit(value); }
public JavaScriptException(JavaScriptValue error) : base(error.ConvertToString().ToString()) { this.error = error; }
public static void Register(JavaScriptContext context) { JavaScriptValue MonoBehaviourPrototype; JavaScriptValue MonoBehaviourConstructor = Bridge.CreateConstructor( typeof(UnityEngine.MonoBehaviour), (args) => { throw new System.NotImplementedException(); }, out MonoBehaviourPrototype ); JavaScriptValue .GlobalObject .GetProperty("UnityEngine") .SetProperty("MonoBehaviour", MonoBehaviourConstructor); // Static Fields // Static Property Accessors // Static Methods MonoBehaviourConstructor.SetProperty( "print", Bridge.CreateFunction( "print", (args) => UnityEngine.MonoBehaviour.print(Bridge.GetExternal <System.Object>(args[1])) ) ); // Instance Fields // Instance Property Accessors Bridge.DefineGetterSetter( MonoBehaviourPrototype, "useGUILayout", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => JavaScriptValue.FromBoolean(o.useGUILayout)), Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => { o.useGUILayout = args[1].ToBoolean(); }) ); #if UNITY_EDITOR Bridge.DefineGetterSetter( MonoBehaviourPrototype, "runInEditMode", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => JavaScriptValue.FromBoolean(o.runInEditMode)), Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => { o.runInEditMode = args[1].ToBoolean(); }) ); #endif // Instance Methods MonoBehaviourPrototype.SetProperty( "IsInvoking", Bridge.CreateFunction( "IsInvoking", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => JavaScriptValue.FromBoolean(o.IsInvoking())) ) ); MonoBehaviourPrototype.SetProperty( "CancelInvoke", Bridge.CreateFunction( "CancelInvoke", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.CancelInvoke()) ) ); MonoBehaviourPrototype.SetProperty( "Invoke", Bridge.CreateFunction( "Invoke", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.Invoke(args[1].ToString(), (float)args[2].ToDouble())) ) ); MonoBehaviourPrototype.SetProperty( "InvokeRepeating", Bridge.CreateFunction( "InvokeRepeating", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.InvokeRepeating(args[1].ToString(), (float)args[2].ToDouble(), (float)args[3].ToDouble())) ) ); MonoBehaviourPrototype.SetProperty( "CancelInvoke", Bridge.CreateFunction( "CancelInvoke", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.CancelInvoke(args[1].ToString())) ) ); MonoBehaviourPrototype.SetProperty( "IsInvoking", Bridge.CreateFunction( "IsInvoking", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => JavaScriptValue.FromBoolean(o.IsInvoking(args[1].ToString()))) ) ); MonoBehaviourPrototype.SetProperty( "StartCoroutine", Bridge.CreateFunction( "StartCoroutine", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => Bridge.CreateExternalWithPrototype(o.StartCoroutine(args[1].ToString()))) ) ); MonoBehaviourPrototype.SetProperty( "StartCoroutine", Bridge.CreateFunction( "StartCoroutine", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => Bridge.CreateExternalWithPrototype(o.StartCoroutine(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))) ) ); MonoBehaviourPrototype.SetProperty( "StartCoroutine", Bridge.CreateFunction( "StartCoroutine", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => Bridge.CreateExternalWithPrototype(o.StartCoroutine(Bridge.GetExternal <System.Collections.IEnumerator>(args[1])))) ) ); MonoBehaviourPrototype.SetProperty( "StopCoroutine", Bridge.CreateFunction( "StopCoroutine", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.StopCoroutine(Bridge.GetExternal <System.Collections.IEnumerator>(args[1]))) ) ); MonoBehaviourPrototype.SetProperty( "StopCoroutine", Bridge.CreateFunction( "StopCoroutine", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.StopCoroutine(Bridge.GetExternal <UnityEngine.Coroutine>(args[1]))) ) ); MonoBehaviourPrototype.SetProperty( "StopCoroutine", Bridge.CreateFunction( "StopCoroutine", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.StopCoroutine(args[1].ToString())) ) ); MonoBehaviourPrototype.SetProperty( "StopAllCoroutines", Bridge.CreateFunction( "StopAllCoroutines", Bridge.WithExternal <UnityEngine.MonoBehaviour>((o, args) => o.StopAllCoroutines()) ) ); }
private JToken VisitBoolean(JavaScriptValue value) { return value.ToBoolean() ? s_true : s_false; }
public static void Register(JavaScriptContext context) { JavaScriptValue CharacterControllerPrototype; JavaScriptValue CharacterControllerConstructor = Bridge.CreateConstructor( typeof(UnityEngine.CharacterController), (args) => { throw new System.NotImplementedException(); }, out CharacterControllerPrototype ); JavaScriptValue .GlobalObject .GetProperty("UnityEngine") .SetProperty("CharacterController", CharacterControllerConstructor); // Static Fields // Static Property Accessors // Static Methods // Instance Fields // Instance Property Accessors Bridge.DefineGetter( CharacterControllerPrototype, "velocity", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => Bridge.CreateExternalWithPrototype(o.velocity)) ); Bridge.DefineGetter( CharacterControllerPrototype, "isGrounded", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromBoolean(o.isGrounded)) ); Bridge.DefineGetter( CharacterControllerPrototype, "collisionFlags", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => Bridge.CreateExternalWithPrototype(o.collisionFlags)) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "radius", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromDouble(o.radius)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.radius = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "height", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromDouble(o.height)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.height = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "center", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => Bridge.CreateExternalWithPrototype(o.center)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.center = Bridge.GetBoxedExternal <UnityEngine.Vector3>(args[1]).wrapped; }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "slopeLimit", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromDouble(o.slopeLimit)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.slopeLimit = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "stepOffset", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromDouble(o.stepOffset)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.stepOffset = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "skinWidth", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromDouble(o.skinWidth)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.skinWidth = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "minMoveDistance", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromDouble(o.minMoveDistance)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.minMoveDistance = (float)args[1].ToDouble(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "detectCollisions", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromBoolean(o.detectCollisions)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.detectCollisions = args[1].ToBoolean(); }) ); Bridge.DefineGetterSetter( CharacterControllerPrototype, "enableOverlapRecovery", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromBoolean(o.enableOverlapRecovery)), Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => { o.enableOverlapRecovery = args[1].ToBoolean(); }) ); // Instance Methods CharacterControllerPrototype.SetProperty( "SimpleMove", Bridge.CreateFunction( "SimpleMove", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => JavaScriptValue.FromBoolean(o.SimpleMove(Bridge.GetBoxedExternal <UnityEngine.Vector3>(args[1]).wrapped))) ) ); CharacterControllerPrototype.SetProperty( "Move", Bridge.CreateFunction( "Move", Bridge.WithExternal <UnityEngine.CharacterController>((o, args) => Bridge.CreateExternalWithPrototype(o.Move(Bridge.GetBoxedExternal <UnityEngine.Vector3>(args[1]).wrapped))) ) ); }
private JToken VisitNull(JavaScriptValue value) { return s_null; }
// Register sync method that need communicate with Formium object. private JavaScriptValue GetTitle(Formium owner, JavaScriptValue[] arguments) { return(JavaScriptValue.CreateString(owner.Subtitle)); }
private JToken VisitObject(JavaScriptValue value) { var jsonObject = new JObject(); var properties = Visit(value.GetOwnPropertyNames()).ToObject<string[]>(); foreach (var property in properties) { var propertyId = JavaScriptPropertyId.FromString(property); var propertyValue = value.GetProperty(propertyId); jsonObject.Add(property, Visit(propertyValue)); } return jsonObject; }
// Register sync method without communicating with Formium object. private JavaScriptValue Test(JavaScriptValue[] arguments) { return(JavaScriptValue.CreateString("OK")); }
/// <summary> /// Initializes a new instance of the <see cref="JavaScriptScriptException"/> class. /// </summary> /// <param name="code">The error code returned.</param> /// <param name="error">The JavaScript error object.</param> /// <param name="message">The error message.</param> public JavaScriptScriptException(JavaScriptErrorCode code, JavaScriptValue error, string message) : base(code, message) { this.error = error; }
private JavaScriptErrorCode ModuleNotifyReady(JavaScriptModuleRecord module, JavaScriptValue value) { // Debug.Log("ModuleNotifyReady"); return(JavaScriptErrorCode.NoError); }