|
27 | 27 | import java.util.List; |
28 | 28 | import java.util.Map; |
29 | 29 | import java.util.Map.Entry; |
| 30 | +import java.util.function.Consumer; |
30 | 31 |
|
| 32 | +import org.apache.commons.lang3.ObjectUtils.Null; |
31 | 33 | import org.apache.commons.logging.Log; |
32 | 34 | import org.apache.commons.logging.LogFactory; |
33 | 35 | import org.htmlunit.BrowserVersion; |
|
59 | 61 | import org.htmlunit.corejs.javascript.TopLevel; |
60 | 62 | import org.htmlunit.corejs.javascript.VarScope; |
61 | 63 | import org.htmlunit.corejs.javascript.WithScope; |
| 64 | +import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer; |
| 65 | +import org.htmlunit.corejs.javascript.typedarrays.NativeUint8Array; |
62 | 66 | import org.htmlunit.html.DomNode; |
63 | 67 | import org.htmlunit.html.HtmlElement; |
64 | 68 | import org.htmlunit.html.HtmlForm; |
@@ -1404,6 +1408,24 @@ public static Scriptable newArray(final Scriptable scope, final Object[] element |
1404 | 1408 | return result; |
1405 | 1409 | } |
1406 | 1410 |
|
| 1411 | + /** |
| 1412 | + * Create an Uint8Array with a specified elements. |
| 1413 | + * |
| 1414 | + * @param scope the scope to create the object in |
| 1415 | + * @param elements the initial elements.. |
| 1416 | + * @return the new Uint8Array |
| 1417 | + */ |
| 1418 | + public static NativeUint8Array newUint8Array(final Scriptable scope, final byte[] elements) { |
| 1419 | + final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(elements.length); |
| 1420 | + ScriptRuntime.setBuiltinProtoAndParent(arrayBuffer, scope, TopLevel.Builtins.ArrayBuffer); |
| 1421 | + System.arraycopy(elements, 0, arrayBuffer.getBuffer(), 0, elements.length); |
| 1422 | + |
| 1423 | + final NativeUint8Array uint8Array = new NativeUint8Array(arrayBuffer, 0, elements.length); |
| 1424 | + ScriptRuntime.setBuiltinProtoAndParent(uint8Array, scope, TopLevel.Builtins.Uint8Array); |
| 1425 | + |
| 1426 | + return uint8Array; |
| 1427 | + } |
| 1428 | + |
1407 | 1429 | /** |
1408 | 1430 | * @param o the object to convert |
1409 | 1431 | * @return int value |
@@ -1471,6 +1493,23 @@ public static long lengthOfArrayLike(final Context cx, final Scriptable obj) { |
1471 | 1493 | return AbstractEcmaObjectOperations.lengthOfArrayLike(cx, obj); |
1472 | 1494 | } |
1473 | 1495 |
|
| 1496 | + /** |
| 1497 | + * Iterates an arrayLike {@link Scriptable} calling the {@link Consumer} on |
| 1498 | + * every item. |
| 1499 | + * @param cx the context or {@link Null} |
| 1500 | + * @param arrayLike the {@link Scriptable} to iterate |
| 1501 | + * @param consumer the {@link Consumer} to call |
| 1502 | + */ |
| 1503 | + public static void iterateArrayLike(final Context cx, final Scriptable arrayLike, final Consumer<Object> consumer) { |
| 1504 | + final Context context = cx == null ? Context.getCurrentContext() : cx; |
| 1505 | + |
| 1506 | + final long len = lengthOfArrayLike(context, arrayLike); |
| 1507 | + for (int i = 0; i < len; i++) { |
| 1508 | + final Object item = arrayLike.get(i, arrayLike); |
| 1509 | + consumer.accept(item); |
| 1510 | + } |
| 1511 | + } |
| 1512 | + |
1474 | 1513 | /** |
1475 | 1514 | * @return the top call scope |
1476 | 1515 | */ |
|
0 commit comments