Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typedarrrayadditions #57

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions runtime/ejs-atoms.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ EJS_ATOM2(bound ,bound_space)

// ArrayBuffer functions
//EJS_ATOM(slice)
EJS_ATOM(isView)

// console functions
//EJS_ATOM(log)
Expand Down
443 changes: 439 additions & 4 deletions runtime/ejs-typedarrays.c

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions runtime/ejs-typedarrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ typedef struct _EJSDataView {
))

#define EJSVAL_TO_TYPEDARRAY(v) ((EJSTypedArray*)EJSVAL_TO_OBJECT(v))
#define EJSVAL_TO_ARRAYBUFFER(v) ((EJSArrayBuffer*)EJSVAL_TO_OBJECT(v))

#define EJSVAL_IS_ARRAYBUFFER(v) (EJSVAL_IS_OBJECT(v) && (EJSVAL_TO_OBJECT(v)->ops == &_ejs_ArrayBuffer_specops))
#define EJSVAL_IS_DATAVIEW(v) (EJSVAL_IS_OBJECT(v) && (EJSVAL_TO_OBJECT(v)->ops == &_ejs_DataView_specops))

#define EJSOBJECT_IS_TYPEDARRAY(v) ((v)->ops == &_ejs_Int8Array_specops || \
(v)->ops == &_ejs_Int16Array_specops || \
Expand Down
10 changes: 10 additions & 0 deletions test/expected/typedarray10.js.expected-out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0
1
2
3
4
101
99
77
67
4
6 changes: 6 additions & 0 deletions test/expected/typedarray11.js.expected-out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{44,101,34,77,1234}
{44;101;34;77;1234}
{44--101--34--77--1234}
{44 101 34 77 1234}
{44010103407701234}
{44 101 34 77 1234}
9 changes: 9 additions & 0 deletions test/expected/typedarray7.js.expected-out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
false
false
false
false
false
false
true
true
true
13 changes: 13 additions & 0 deletions test/expected/typedarray8.js.expected-out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1
2
3
0
0
0
0
0

1
2
3
0
6 changes: 6 additions & 0 deletions test/expected/typedarray9.js.expected-out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a[0] = 2
a[1] = 5
a[2] = 9
a[0] = 7
a[1] = 0
a[2] = 120
13 changes: 13 additions & 0 deletions test/typedarray10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var arr = Int32Array.of(101, 99, 77, 67, 4);
var iter;

// 1. Go for keys()
var keys = arr.keys();
while (!(iter = keys.next ()).done)
console.log (iter.value);

// 2. Go for values()
var values = arr.values();
while (!(iter = values.next ()).done)
console.log (iter.value);

9 changes: 9 additions & 0 deletions test/typedarray11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

let arr = Int32Array.of(44, 101, 34, 77, 1234);
console.log ("{" + arr.join() + "}");
console.log ("{" + arr.join(";") + "}");
console.log ("{" + arr.join("--") + "}");
console.log ("{" + arr.join(" ") + "}");
console.log ("{" + arr.join(0) + "}");
console.log ("{" + arr.join(" ") + "}");

16 changes: 16 additions & 0 deletions test/typedarray7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// From the MDN samples.

console.log(ArrayBuffer.isView()); // false
console.log(ArrayBuffer.isView([])); // false
console.log(ArrayBuffer.isView({})); // false
console.log(ArrayBuffer.isView(null)); // false
console.log(ArrayBuffer.isView(undefined)); // false
console.log(ArrayBuffer.isView(new ArrayBuffer(10))); // false

console.log(ArrayBuffer.isView(new Uint8Array())); // true
console.log(ArrayBuffer.isView(new Float32Array())); // true
//ArrayBuffer.isView(new Int8Array(10).subarray(0, 3)); // true

var buffer = new ArrayBuffer(2);
var dv = new DataView(buffer);
console.log(ArrayBuffer.isView(dv)); // true
19 changes: 19 additions & 0 deletions test/typedarray8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

var buffer = new ArrayBuffer(8);
var uint8 = new Uint8Array(buffer);
uint8[0] = 1;
uint8[1] = 2;
uint8[2] = 3;

// [ 1, 2, 3, 0, 0, 0, 0, 0 ]
for (let i = 0; i < uint8.length; i++)
console.log(uint8[i]);

console.log();

var sub = uint8.subarray(0, 4);

// [ 1, 2, 3, 0 ]
for (let i = 0; i < sub.length; i++)
console.log(sub[i]);

25 changes: 25 additions & 0 deletions test/typedarray9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}

let arr = new Int32Array(3);
arr[0] = 2;
arr[1] = 5;
arr[2] = 9;

arr.forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9

let arr2 = new Int8Array(3);
arr2[0] = 7;
arr2[2] = 120;

arr2.forEach(logArrayElements);
// logs:
// a[0] = 7
// a[1] = 0
// a[2] = 120