Luke Briggs

JS API improvements - adding in typed arrays (aurora.js)

Showing 33 changed files with 1902 additions and 13 deletions
......@@ -40,9 +40,21 @@ namespace PowerUI{
Engine.SetValue("document", doc)
.SetValue("Promise", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Promise)))
.SetValue("ArrayBuffer", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.ArrayBuffer)))
.SetValue("DataView", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.DataView)))
.SetValue("Float32Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Float32Array)))
.SetValue("Float64Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Float64Array)))
.SetValue("Int8Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Int8Array)))
.SetValue("Int16Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Int16Array)))
.SetValue("Int32Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Int32Array)))
.SetValue("Uint8ClampedArray", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Uint8ClampedArray)))
.SetValue("Uint8Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Uint8Array)))
.SetValue("Uint16Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Uint16Array)))
.SetValue("Uint32Array", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.Uint32Array)))
.SetValue("XMLHttpRequest", TypeReference.CreateTypeReference(Engine, typeof(PowerUI.XMLHttpRequest)))
.SetValue("Module", TypeReference.CreateTypeReference(Engine, typeof(WebAssembly.Module)))
.SetValue("console", new JavaScript.console())
.SetValue("window", Engine.Global)
.SetValue("PowerUI", new NamespaceReference(Engine, "PowerUI"));
var coreWindow = (window as PowerUI.Window);
......
......@@ -55,15 +55,16 @@ namespace Jint.Runtime.Interop
var constructors = Type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
var methods = TypeConverter.FindBestMatch(Engine, constructors, arguments).ToList();
var parameters = new object[arguments.Length];
foreach (var method in methods)
{
var parameters = new object[arguments.Length];
var _methodParams = method.GetParameters();
try
{
for (var i = 0; i < arguments.Length; i++)
{
var parameterType = method.GetParameters()[i].ParameterType;
var parameterType = _methodParams[i].ParameterType;
if (parameterType == typeof(JsValue))
{
......@@ -77,21 +78,22 @@ namespace Jint.Runtime.Interop
CultureInfo.InvariantCulture);
}
}
var constructor = (ConstructorInfo)method;
var instance = constructor.Invoke(parameters.ToArray());
var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
// todo: cache method info
return result;
}
}
catch
{
// ignore method
continue;
}
}
var constructor = (ConstructorInfo)method;
var instance = constructor.Invoke(parameters.ToArray());
var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
// todo: cache method info
return result;
}
throw new JavaScriptException(Engine.TypeError, "No public methods with the specified arguments were found.");
}
......
......@@ -7,7 +7,12 @@ namespace Jint.Runtime
public class JavaScriptException : Exception
{
private readonly JsValue _errorObject;
public JavaScriptException(string type, string message) : base("")
{
_errorObject = new JsValue(type+ " : " + message);
}
public JavaScriptException(ErrorConstructor errorConstructor) : base("")
{
_errorObject = errorConstructor.Construct(Arguments.Empty);
......
......@@ -219,6 +219,16 @@ namespace Jint.Runtime
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static short ToInt16(JsValue o)
{
return (short)(uint)ToNumber(o);
}
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static int ToInt32(JsValue o)
{
return (int)(uint)ToNumber(o);
......@@ -239,6 +249,26 @@ namespace Jint.Runtime
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static sbyte ToInt8(JsValue o)
{
return (sbyte)(uint)ToNumber(o);
}
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static byte ToUint8(JsValue o)
{
return (byte)(uint)ToNumber(o);
}
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static ushort ToUint16(JsValue o)
{
return (ushort)(uint)ToNumber(o);
......
fileFormatVersion: 2
guid: 689bf56fbbb90024283a64315a327a02
folderAsset: yes
timeCreated: 1508622778
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
using System;
namespace PowerUI
{
/// <summary>
/// The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer.
/// You can not directly manipulate the contents of an ArrayBuffer; instead, you create one of
/// the typed array objects or a DataView object which represents the buffer in a specific
/// format, and use that to read and write the contents of the buffer.
/// </summary>
public class ArrayBuffer
{
/// <summary>
/// The raw buffer.
/// </summary>
internal byte[] buffer;
// INITIALIZATION
//_________________________________________________________________________________________
/// <summary>
/// Creates a new ArrayBuffer instance.
/// </summary>
/// <param name="prototype"> The next object in the prototype chain. </param>
/// <param name="size"> The size, in bytes, of the array buffer to create. </param>
public ArrayBuffer(int size)
{
this.buffer = new byte[size];
}
/// <summary>
/// Creates a new ArrayBuffer instance from an existing buffer.
/// </summary>
/// <param name="prototype"> The next object in the prototype chain. </param>
/// <param name="buffer"> The buffer to use. </param>
private ArrayBuffer(byte[] buffer)
{
this.buffer = buffer;
}
// .NET ACCESSOR PROPERTIES
//_________________________________________________________________________________________
/// <summary>
/// Gets the internal array for this ArrayBuffer.
/// </summary>
internal byte[] Buffer
{
get { return this.buffer; }
}
// JAVASCRIPT PROPERTIES
//_________________________________________________________________________________________
/// <summary>
/// Gets the length of an ArrayBuffer in bytes. Returns 0 if this ArrayBuffer has been
/// detached.
/// </summary>
public int ByteLength
{
get { return this.buffer.Length; }
}
// JAVASCRIPT FUNCTIONS
//_________________________________________________________________________________________
/// <summary>
/// Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from
/// begin, inclusive, up to end, exclusive.
/// </summary>
/// <param name="begin"> Zero-based byte index at which to begin slicing. </param>
/// <returns> A new ArrayBuffer object. </returns>
public ArrayBuffer Slice(int begin)
{
return Slice(begin, this.buffer.Length);
}
/// <summary>
/// Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from
/// begin, inclusive, up to end, exclusive.
/// </summary>
/// <param name="begin"> Zero-based byte index at which to begin slicing. </param>
/// <param name="end"> Byte index to end slicing. If end is unspecified, the new
/// ArrayBuffer contains all bytes from begin to the end of this ArrayBuffer. The range
/// specified by the begin and end values is clamped to the valid index range for the
/// current array. If the computed length of the new ArrayBuffer would be negative, it is
/// clamped to zero. </param>
/// <returns> A new ArrayBuffer object. </returns>
public ArrayBuffer Slice(int begin, int end)
{
// 1. Let O be the this value.
// 2. If Type(O) is not Object, throw a TypeError exception.
// 3. If O does not have an[[ArrayBufferData]] internal slot, throw a TypeError exception.
// 4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
// 5. Let len be the value of O’s[[ArrayBufferByteLength]] internal slot.
// 6. Let relativeStart be ToInteger(start).
// 7. ReturnIfAbrupt(relativeStart).
// 8. If relativeStart< 0, let first be max((len + relativeStart),0); else let first be min(relativeStart, len).
// 9. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
// 10. ReturnIfAbrupt(relativeEnd).
// 11. If relativeEnd< 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
// 12. Let newLen be max(final-first,0).
// 13. Let ctor be SpeciesConstructor(O, %ArrayBuffer%).
// 14. ReturnIfAbrupt(ctor).
// 15. Let new be Construct(ctor, «newLen»).
// 16. ReturnIfAbrupt(new).
// 17. If new does not have an[[ArrayBufferData]] internal slot, throw a TypeError exception.
// 18. If IsDetachedBuffer(new) is true, throw a TypeError exception.
// 19. If SameValue(new, O) is true, throw a TypeError exception.
// 20. If the value of new’s[[ArrayBufferByteLength]] internal slot<newLen, throw a TypeError exception.
// 21. NOTE: Side-effects of the above steps may have detached O.
// 22. If IsDetachedBuffer(O) is true, throw a TypeError exception.
// 23. Let fromBuf be the value of O’s[[ArrayBufferData]] internal slot.
// 24. Let toBuf be the value of new’s[[ArrayBufferData]] internal slot.
// 25. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
// 26. Return new.
if (begin < 0)
begin += this.buffer.Length;
begin = Math.Min(Math.Max(begin, 0), this.buffer.Length);
if (end < 0)
end += this.buffer.Length;
end = Math.Min(Math.Max(end, begin), this.buffer.Length);
byte[] newBuffer = new byte[end - begin];
System.Array.Copy(this.buffer, begin, newBuffer, 0, end - begin);
return null;//new ArrayBuffer(newBuffer);
}
// JAVASCRIPT FUNCTIONS
//_________________________________________________________________________________________
/// <summary>
/// Returns <c>true</c> if <paramref name="arg"/> is one of the ArrayBuffer views, such as
/// typed array objects or a DataView; <c>false</c> otherwise.
/// </summary>
/// <param name="arg"> The argument to be checked. </param>
/// <returns> <c>true</c> if arg is one of the ArrayBuffer views. </returns>
public static bool IsView(object arg)
{
return arg is DataView || arg is TypedArray;
}
}
}
fileFormatVersion: 2
guid: 94e87a3baba33ab4081e456ed26361cb
timeCreated: 1508622778
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: bd3147617d931044f9b183348de277e9
timeCreated: 1508622778
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace PowerUI
{
/// <summary>
/// A float32 array.
/// </summary>
public class Float32Array : TypedArray
{
/// <summary>An internal buffer.</summary>
private float[] rawBuffer;
// new Float32Array(length);
// new Float32Array(typedArray);
// new Float32Array(object);
// new Float32Array(buffer[, byteOffset[, length]]);
public Float32Array(int length):base(TypedArrayStyle.Float32Array, length){
// Create the fast buffer:
rawBuffer=new float[Length];
}
public Float32Array(double length):base(TypedArrayStyle.Float32Array, (int)length){
// Create the fast buffer:
rawBuffer=new float[Length];
}
public Float32Array(TypedArray array):base(TypedArrayStyle.Float32Array, array)
{
// Create the quick buffer:
rawBuffer=new float[Length];
// Now add the array:
Add(array);
}
public Float32Array(object iterableObj):base(TypedArrayStyle.Float32Array, iterableObj)
{
// Create the quick buffer:
rawBuffer=new float[Length];
// Now add it:
Add(iterableObj);
}
public Float32Array(ArrayBuffer buff):this(buff,0,0){}
public Float32Array(ArrayBuffer buff,int byteOffset,int length):base(TypedArrayStyle.Float32Array, length==0?buff.ByteLength:length)
{
ByteOffset=byteOffset;
_Buffer=buff;
}
protected override void FillBuffer()
{
int length=Length;
int byteOffset=ByteOffset;
for(int i=0;i<length;i++)
{
var value=rawBuffer[i];
byte[] bytes=BitConverter.GetBytes(value);
System.Array.Copy(bytes,0,_Buffer.buffer,byteOffset,4);
byteOffset+=4;
}
// Remove the fast buffer:
rawBuffer=null;
}
/// <summary>Gets the value at the given index.</summary>
protected override object Get(int index)
{
if(rawBuffer==null)
{
// Use the _Buffer instead:
return BitConverter.ToSingle(_Buffer.buffer,(index * 4) + ByteOffset);
}
return rawBuffer[index];
}
/// <summary>
/// Puts an unknown object into this array.
/// Note that the value is always expected to be a value type.
/// </summary>
protected override void Set(int index,object value)
{
float sValue = (value is double) ? (float)((double)value) : (float)value;
if(rawBuffer==null)
{
// Use the _Buffer instead (this is unfortunate!):
byte[] bytes=BitConverter.GetBytes(sValue);
System.Array.Copy(bytes,0,_Buffer.buffer,(index * 4) + ByteOffset,4);
return;
}
// Get it as an int and put it in:
rawBuffer[index]=sValue;
}
/// <summary>
/// Gets or sets the given entry in the array.
/// </summary>
public float this[int index]{
get{
if(rawBuffer==null)
{
// Use the _Buffer instead:
return BitConverter.ToSingle(_Buffer.buffer,(index * 4) + ByteOffset);
}
return rawBuffer[index];
}
set{
if(rawBuffer==null)
{
// Use the _Buffer instead (this is unfortunate!):
byte[] bytes=BitConverter.GetBytes(value);
System.Array.Copy(bytes,0,_Buffer.buffer,(index * 4) + ByteOffset,4);
return;
}
rawBuffer[index]=value;
}
}
/// <summary>Creates a Float32Array from the given iterable object.</summary>
public static Float32Array From(object value)
{
return new Float32Array(value);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: e2c2a57f30a667d47ae156516a5736e6
timeCreated: 1508622778
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace PowerUI
{
/// <summary>
/// A double32 array.
/// </summary>
public class Float64Array : TypedArray
{
/// <summary>An internal buffer.</summary>
private double[] rawBuffer;
// new Float64Array(length);
// new Float64Array(typedArray);
// new Float64Array(object);
// new Float64Array(buffer[, byteOffset[, length]]);
public Float64Array(int length):base(TypedArrayStyle.Float64Array, length){
// Create the fast buffer:
rawBuffer=new double[Length];
}
public Float64Array(double length):base(TypedArrayStyle.Float64Array, (int)length){
// Create the fast buffer:
rawBuffer=new double[Length];
}
public Float64Array(TypedArray array):base(TypedArrayStyle.Float64Array, array)
{
// Create the quick buffer:
rawBuffer=new double[Length];
// Now add the array:
Add(array);
}
public Float64Array(object iterableObj):base(TypedArrayStyle.Float64Array, iterableObj)
{
// Create the quick buffer:
rawBuffer=new double[Length];
// Now add it:
Add(iterableObj);
}
public Float64Array(ArrayBuffer buff):this(buff,0,0){}
public Float64Array(ArrayBuffer buff,int byteOffset,int length):base(TypedArrayStyle.Float64Array, length==0?buff.ByteLength:length)
{
ByteOffset=byteOffset;
_Buffer=buff;
}
protected override void FillBuffer()
{
int length=Length;
int byteOffset=ByteOffset;
for(int i=0;i<length;i++)
{
var value=rawBuffer[i];
byte[] bytes=BitConverter.GetBytes(value);
System.Array.Copy(bytes,0,_Buffer.buffer,byteOffset,8);
byteOffset+=8;
}
// Remove the fast buffer:
rawBuffer=null;
}
/// <summary>Gets the value at the given index.</summary>
protected override object Get(int index)
{
if(rawBuffer==null)
{
// Use the _Buffer instead:
return BitConverter.ToDouble(_Buffer.buffer,(index * 8) + ByteOffset);
}
return rawBuffer[index];
}
/// <summary>
/// Puts an unknown object into this array.
/// Note that the value is always expected to be a value type.
/// </summary>
protected override void Set(int index,object value)
{
double sValue = (double)value;
if(rawBuffer==null)
{
// Use the _Buffer instead (this is unfortunate!):
byte[] bytes=BitConverter.GetBytes(sValue);
System.Array.Copy(bytes,0,_Buffer.buffer,(index * 8) + ByteOffset,8);
return;
}
// Get it as an int and put it in:
rawBuffer[index]=sValue;
}
/// <summary>
/// Gets or sets the given entry in the array.
/// </summary>
public double this[int index]{
get{
if(rawBuffer==null)
{
// Use the _Buffer instead:
return BitConverter.ToDouble(_Buffer.buffer,(index * 8) + ByteOffset);
}
return rawBuffer[index];
}
set{
if(rawBuffer==null)
{
// Use the _Buffer instead (this is unfortunate!):
byte[] bytes=BitConverter.GetBytes(value);
System.Array.Copy(bytes,0,_Buffer.buffer,(index * 8) + ByteOffset,8);
return;
}
rawBuffer[index]=value;
}
}
/// <summary>Creates a Float64Array from the given iterable object.</summary>
public static Float64Array From(object value)
{
return new Float64Array(value);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 6bc688e32bf62c642951fbe269fe48a4
timeCreated: 1508622778
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace PowerUI
{
/// <summary>
/// An int16 array.
/// </summary>
public class Int16Array : TypedArray
{
/// <summary>An internal buffer.</summary>
private short[] rawBuffer;
// new Int16Array(length);
// new Int16Array(typedArray);
// new Int16Array(object);
// new Int16Array(buffer[, byteOffset[, length]]);
public Int16Array(int length):base(TypedArrayStyle.Int16Array, length){
// Create the fast buffer:
rawBuffer=new short[Length];
}
public Int16Array(double length):base(TypedArrayStyle.Int16Array, (int)length){
// Create the fast buffer:
rawBuffer=new short[Length];
}
public Int16Array(TypedArray array):base(TypedArrayStyle.Int16Array, array)
{
// Create the quick buffer:
rawBuffer=new short[Length];
// Now add the array:
Add(array);
}
public Int16Array(object iterableObj):base(TypedArrayStyle.Int16Array, iterableObj)
{
// Create the quick buffer:
rawBuffer=new short[Length];
// Now add it:
Add(iterableObj);
}
public Int16Array(ArrayBuffer buff):this(buff,0,0){}
public Int16Array(ArrayBuffer buff,int byteOffset,int length):base(TypedArrayStyle.Int16Array, length==0?buff.ByteLength:length)
{
ByteOffset=byteOffset;
_Buffer=buff;
}
protected override void FillBuffer()
{
int length=Length;
int byteOffset=ByteOffset;
for(int i=0;i<length;i++)
{
var value=rawBuffer[i];
LittleConverter.GetBytes(value,_Buffer.buffer,byteOffset);
byteOffset+=2;
}
// Remove the fast buffer:
rawBuffer=null;
}
/// <summary>Gets the value at the given index.</summary>
protected override object Get(int index)
{
if(rawBuffer==null)
{
// Use the _Buffer instead:
return LittleConverter.ToInt16(_Buffer.buffer,(index * 2) + ByteOffset);
}
return rawBuffer[index];
}
/// <summary>
/// Puts an unknown object into this array.
/// Note that the value is always expected to be a value type.
/// </summary>
protected override void Set(int index,object value)
{
short sValue = (value is double) ? (short)((double)value) : (short)value;
if(rawBuffer==null)
{
// Use the _Buffer instead:
LittleConverter.GetBytes(sValue,_Buffer.buffer,(index * 2) + ByteOffset);
return;
}
// Get it as a short and put it in:
rawBuffer[index]=sValue;
}
/// <summary>
/// Gets or sets the given entry in the array.
/// </summary>
public short this[int index]{
get{
if(rawBuffer==null)
{
// Use the _Buffer instead:
return LittleConverter.ToInt16(_Buffer.buffer,(index * 2) + ByteOffset);
}
return rawBuffer[index];
}
set{
if(rawBuffer==null)
{
// Use the _Buffer instead:
LittleConverter.GetBytes(value,_Buffer.buffer,(index * 2) + ByteOffset);
return;
}
rawBuffer[index]=value;
}
}
/// <summary>Creates a Int16Array from the given iterable object.</summary>
public static Int16Array From(object value)
{
return new Int16Array(value);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: e3ec02f1794cbe14dbc49390554c74af
timeCreated: 1508622778
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace PowerUI
{
/// <summary>
/// An int32 array.
/// </summary>
public class Int32Array : TypedArray
{
/// <summary>An internal buffer.</summary>
private int[] rawBuffer;
// new Int32Array(length);
// new Int32Array(typedArray);
// new Int32Array(object);
// new Int32Array(buffer[, byteOffset[, length]]);
public Int32Array(int length):base(TypedArrayStyle.Int32Array, length){
// Create the fast buffer:
rawBuffer=new int[Length];
}
public Int32Array(double length):base(TypedArrayStyle.Int32Array, (int)length){
// Create the fast buffer: