private object[] Translate(object[] args) { var actualArgs = new object[args.Length]; for (int i = 0; i < actualArgs.Length; i++) { var arg = args[i]; if (arg == null) { actualArgs[i] = null; continue; } if (arg is JSHandler jsHandler) { arg = jsHandler.Handle; } else if (arg is System.Array array) { if (array.GetType().GetElementType().IsPrimitive) { arg = CastNativeArray(array); } else { arg = new WebAssembly.Core.Array(array); } } actualArgs[i] = arg; } return(actualArgs); }
public static object FunctionMathMin(WebAssembly.Core.Array array) { object[] parms = new object[array.Length]; for (int x = 0; x < array.Length; x++) { parms[x] = array[x]; } var math = (JSObject)Runtime.GetGlobalObject("Math"); var min = (Function)math.GetObjectProperty("min"); return(min.Apply(null, parms)); }
public static T[] ToArray <T>(this WebAssembly.Core.Array array, Func <object, T> cast) { if (array == null) { return(null); } var length = array.Length; var result = new T[length]; for (int i = 0; i < length; i++) { result[i] = cast(array[i]); } return(result); }
private object[] Translate(object[] args) { var actualArgs = new object[args.Length]; for (int i = 0; i < actualArgs.Length; i++) { var arg = args[i]; if (arg == null) { actualArgs[i] = null; continue; } if (arg is JSHandler jsHandler) { arg = jsHandler.Handle; } else if (arg is System.Array array) { if (((System.Array)arg).GetType().GetElementType().IsPrimitive) { arg = CastNativeArray(array); } else { // WebAssembly.Core.Array or Runtime should probably provide some type of // helper functions for doing this. I will put it on my todo list. var argArray = new WebAssembly.Core.Array(); foreach (var item in (System.Array)arg) { argArray.Push(item); } arg = argArray; } } actualArgs[i] = arg; } return(actualArgs); }
public static object ArrayPop() { var arr = new WebAssembly.Core.Array(); return(arr.Pop()); }
public void SetWeatherCycle(int[] weathers, int[] multipliers) { setWeatherCycle.Call(alt, new Array(weathers), new Array(multipliers)); }
private async Task doFetch(TaskCompletionSource <HttpResponseMessage> tcs, HttpRequestMessage request, CancellationToken cancellationToken) { try { var requestObject = new JSObject(); if (request.Properties.TryGetValue("WebAssemblyFetchOptions", out var fetchOoptionsValue) && fetchOoptionsValue is IDictionary <string, object> fetchOptions) { foreach (var item in fetchOptions) { requestObject.SetObjectProperty(item.Key, item.Value); } } requestObject.SetObjectProperty("method", request.Method.Method); // We need to check for body content if (request.Content != null) { if (request.Content is StringContent) { requestObject.SetObjectProperty("body", await request.Content.ReadAsStringAsync()); } else { // 2.1.801 seems to have a problem with the line // using (var uint8Buffer = Uint8Array.From(await request.Content.ReadAsByteArrayAsync ())) // so we split it up into two lines. var byteAsync = await request.Content.ReadAsByteArrayAsync(); using (var uint8Buffer = Uint8Array.From(byteAsync)) { requestObject.SetObjectProperty("body", uint8Buffer); } } } // Process headers // Cors has it's own restrictions on headers. // https://developer.mozilla.org/en-US/docs/Web/API/Headers using (var jsHeaders = new HostObject("Headers")) { if (request.Headers != null) { foreach (var header in request.Headers) { foreach (var value in header.Value) { jsHeaders.Invoke("append", header.Key, value); } } } if (request.Content?.Headers != null) { foreach (var header in request.Content.Headers) { foreach (var value in header.Value) { jsHeaders.Invoke("append", header.Key, value); } } } requestObject.SetObjectProperty("headers", jsHeaders); } WasmHttpReadStream wasmHttpReadStream = null; JSObject abortController = new HostObject("AbortController"); JSObject signal = (JSObject)abortController.GetObjectProperty("signal"); requestObject.SetObjectProperty("signal", signal); signal.Dispose(); CancellationTokenSource abortCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); CancellationTokenRegistration abortRegistration = abortCts.Token.Register((Action)(() => { if (abortController.JSHandle != -1) { abortController.Invoke("abort"); abortController?.Dispose(); } wasmHttpReadStream?.Dispose(); })); var args = new WebAssembly.Core.Array(); args.Push(request.RequestUri.ToString()); args.Push(requestObject); requestObject.Dispose(); var response = fetch.Invoke("apply", window, args) as Task <object>; args.Dispose(); if (response == null) { throw new Exception("Internal error marshalling the response Promise from `fetch`."); } var t = await response; var status = new WasmFetchResponse((JSObject)t, abortController, abortCts, abortRegistration); //Console.WriteLine($"bodyUsed: {status.IsBodyUsed}"); //Console.WriteLine($"ok: {status.IsOK}"); //Console.WriteLine($"redirected: {status.IsRedirected}"); //Console.WriteLine($"status: {status.Status}"); //Console.WriteLine($"statusText: {status.StatusText}"); //Console.WriteLine($"type: {status.ResponseType}"); //Console.WriteLine($"url: {status.Url}"); HttpResponseMessage httpresponse = new HttpResponseMessage((HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), status.Status.ToString())); var streamingEnabled = request.Properties.TryGetValue("WebAssemblyEnableStreamingResponse", out var streamingEnabledValue) && (bool)streamingEnabledValue; httpresponse.Content = StreamingSupported && streamingEnabled ? new StreamContent(wasmHttpReadStream = new WasmHttpReadStream(status)) : (HttpContent) new WasmHttpContent(status); // Fill the response headers // CORS will only allow access to certain headers. // If a request is made for a resource on another origin which returns the CORs headers, then the type is cors. // cors and basic responses are almost identical except that a cors response restricts the headers you can view to // `Cache-Control`, `Content-Language`, `Content-Type`, `Expires`, `Last-Modified`, and `Pragma`. // View more information https://developers.google.com/web/updates/2015/03/introduction-to-fetch#response_types // // Note: Some of the headers may not even be valid header types in .NET thus we use TryAddWithoutValidation using (var respHeaders = (JSObject)status.Headers) { if (respHeaders != null) { using (var entriesIterator = (JSObject)respHeaders.Invoke("entries")) { JSObject nextResult = null; try { nextResult = (JSObject)entriesIterator.Invoke("next"); while (!(bool)nextResult.GetObjectProperty("done")) { using (var resultValue = (WebAssembly.Core.Array)nextResult.GetObjectProperty("value")) { var name = (string)resultValue [0]; var value = (string)resultValue [1]; if (!httpresponse.Headers.TryAddWithoutValidation(name, value)) { if (httpresponse.Content != null) { if (!httpresponse.Content.Headers.TryAddWithoutValidation(name, value)) { Console.WriteLine($"Warning: Can not add response header for name: {name} value: {value}"); } } } } nextResult?.Dispose(); nextResult = (JSObject)entriesIterator.Invoke("next"); } } finally { nextResult?.Dispose(); } } } } tcs.SetResult(httpresponse); } catch (WebAssembly.JSException jsExc) { var httpExc = new System.Net.Http.HttpRequestException(jsExc.Message); tcs.SetException(httpExc); } catch (Exception exception) { tcs.SetException(exception); } }