Skip to content

Commit 5f26557

Browse files
committed
use our own JavaScriptEngine.newObject(),
introduce JavaScriptEngine.iterateArrayLike() and JavaScriptEngine.newUint8Array()
1 parent a642231 commit 5f26557

7 files changed

Lines changed: 56 additions & 41 deletions

File tree

src/main/java/org/htmlunit/javascript/JavaScriptEngine.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import java.util.List;
2828
import java.util.Map;
2929
import java.util.Map.Entry;
30+
import java.util.function.Consumer;
3031

32+
import org.apache.commons.lang3.ObjectUtils.Null;
3133
import org.apache.commons.logging.Log;
3234
import org.apache.commons.logging.LogFactory;
3335
import org.htmlunit.BrowserVersion;
@@ -59,6 +61,8 @@
5961
import org.htmlunit.corejs.javascript.TopLevel;
6062
import org.htmlunit.corejs.javascript.VarScope;
6163
import org.htmlunit.corejs.javascript.WithScope;
64+
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
65+
import org.htmlunit.corejs.javascript.typedarrays.NativeUint8Array;
6266
import org.htmlunit.html.DomNode;
6367
import org.htmlunit.html.HtmlElement;
6468
import org.htmlunit.html.HtmlForm;
@@ -1404,6 +1408,24 @@ public static Scriptable newArray(final Scriptable scope, final Object[] element
14041408
return result;
14051409
}
14061410

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+
14071429
/**
14081430
* @param o the object to convert
14091431
* @return int value
@@ -1471,6 +1493,23 @@ public static long lengthOfArrayLike(final Context cx, final Scriptable obj) {
14711493
return AbstractEcmaObjectOperations.lengthOfArrayLike(cx, obj);
14721494
}
14731495

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+
14741513
/**
14751514
* @return the top call scope
14761515
*/

src/main/java/org/htmlunit/javascript/host/crypto/AesKeyAlgorithm.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
import java.util.Set;
1818

19-
import org.htmlunit.corejs.javascript.NativeObject;
20-
import org.htmlunit.corejs.javascript.ScriptRuntime;
2119
import org.htmlunit.corejs.javascript.Scriptable;
2220
import org.htmlunit.corejs.javascript.ScriptableObject;
23-
import org.htmlunit.corejs.javascript.TopLevel;
21+
import org.htmlunit.javascript.JavaScriptEngine;
2422

2523
/**
2624
* Internal helper representing AES key algorithm parameters.
@@ -89,8 +87,7 @@ int getLength() {
8987
* @return the JS algorithm object
9088
*/
9189
Scriptable toScriptableObject(final Scriptable scope) {
92-
final NativeObject algorithm = new NativeObject();
93-
ScriptRuntime.setBuiltinProtoAndParent(algorithm, scope, TopLevel.Builtins.Object);
90+
final Scriptable algorithm = JavaScriptEngine.newObject(scope);
9491
ScriptableObject.putProperty(algorithm, "name", getName());
9592
ScriptableObject.putProperty(algorithm, "length", getLength());
9693
return algorithm;

src/main/java/org/htmlunit/javascript/host/crypto/EcKeyAlgorithm.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
import java.util.Map;
1818
import java.util.Set;
1919

20-
import org.htmlunit.corejs.javascript.NativeObject;
21-
import org.htmlunit.corejs.javascript.ScriptRuntime;
2220
import org.htmlunit.corejs.javascript.Scriptable;
2321
import org.htmlunit.corejs.javascript.ScriptableObject;
24-
import org.htmlunit.corejs.javascript.TopLevel;
22+
import org.htmlunit.javascript.JavaScriptEngine;
2523

2624
/**
2725
* Internal helper representing EC key algorithm parameters.
@@ -102,8 +100,7 @@ String getJavaCurveName() {
102100
* @return the JS algorithm object
103101
*/
104102
Scriptable toScriptableObject(final Scriptable scope) {
105-
final NativeObject algorithm = new NativeObject();
106-
ScriptRuntime.setBuiltinProtoAndParent(algorithm, scope, TopLevel.Builtins.Object);
103+
final Scriptable algorithm = JavaScriptEngine.newObject(scope);
107104
ScriptableObject.putProperty(algorithm, "name", getName());
108105
ScriptableObject.putProperty(algorithm, "namedCurve", getNamedCurve());
109106
return algorithm;

src/main/java/org/htmlunit/javascript/host/crypto/HmacKeyAlgorithm.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
import java.util.Set;
1818

19-
import org.htmlunit.corejs.javascript.NativeObject;
20-
import org.htmlunit.corejs.javascript.ScriptRuntime;
2119
import org.htmlunit.corejs.javascript.Scriptable;
2220
import org.htmlunit.corejs.javascript.ScriptableObject;
23-
import org.htmlunit.corejs.javascript.TopLevel;
21+
import org.htmlunit.javascript.JavaScriptEngine;
2422

2523
/**
2624
* Internal helper representing HMAC key algorithm parameters.
@@ -118,12 +116,10 @@ String getJavaName() {
118116
* @return the JS algorithm object
119117
*/
120118
Scriptable toScriptableObject(final Scriptable scope) {
121-
final NativeObject hashObj = new NativeObject();
122-
ScriptRuntime.setBuiltinProtoAndParent(hashObj, scope, TopLevel.Builtins.Object);
119+
final Scriptable hashObj = JavaScriptEngine.newObject(scope);
123120
ScriptableObject.putProperty(hashObj, "name", getHash());
124121

125-
final NativeObject algorithm = new NativeObject();
126-
ScriptRuntime.setBuiltinProtoAndParent(algorithm, scope, TopLevel.Builtins.Object);
122+
final Scriptable algorithm = JavaScriptEngine.newObject(scope);
127123
ScriptableObject.putProperty(algorithm, "name", "HMAC");
128124
ScriptableObject.putProperty(algorithm, "hash", hashObj);
129125
ScriptableObject.putProperty(algorithm, "length", getLength());

src/main/java/org/htmlunit/javascript/host/crypto/RsaHashedKeyAlgorithm.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
import java.math.BigInteger;
1818
import java.util.Set;
1919

20-
import org.htmlunit.corejs.javascript.NativeObject;
21-
import org.htmlunit.corejs.javascript.ScriptRuntime;
2220
import org.htmlunit.corejs.javascript.Scriptable;
2321
import org.htmlunit.corejs.javascript.ScriptableObject;
24-
import org.htmlunit.corejs.javascript.TopLevel;
2522
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
2623
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBufferView;
2724
import org.htmlunit.corejs.javascript.typedarrays.NativeUint8Array;
25+
import org.htmlunit.javascript.JavaScriptEngine;
2826

2927
/**
3028
* Internal helper representing RSA hashed key algorithm parameters.
@@ -150,18 +148,12 @@ String getJavaHash() {
150148
* @return the JS algorithm object
151149
*/
152150
Scriptable toScriptableObject(final Scriptable scope) {
153-
final NativeObject hashObj = new NativeObject();
154-
ScriptRuntime.setBuiltinProtoAndParent(hashObj, scope, TopLevel.Builtins.Object);
151+
final Scriptable hashObj = JavaScriptEngine.newObject(scope);
155152
ScriptableObject.putProperty(hashObj, "name", getHash());
156153

157-
final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(publicExponent_.length);
158-
System.arraycopy(publicExponent_, 0, arrayBuffer.getBuffer(), 0, publicExponent_.length);
159-
ScriptRuntime.setBuiltinProtoAndParent(arrayBuffer, scope, TopLevel.Builtins.ArrayBuffer);
160-
final NativeUint8Array uint8Array = new NativeUint8Array(arrayBuffer, 0, publicExponent_.length);
161-
ScriptRuntime.setBuiltinProtoAndParent(uint8Array, scope, TopLevel.Builtins.Uint8Array);
154+
final NativeUint8Array uint8Array = JavaScriptEngine.newUint8Array(scope, publicExponent_);
162155

163-
final NativeObject algorithm = new NativeObject();
164-
ScriptRuntime.setBuiltinProtoAndParent(algorithm, scope, TopLevel.Builtins.Object);
156+
final Scriptable algorithm = JavaScriptEngine.newObject(scope);
165157
ScriptableObject.putProperty(algorithm, "name", getName());
166158
ScriptableObject.putProperty(algorithm, "hash", hashObj);
167159
ScriptableObject.putProperty(algorithm, "modulusLength", getModulusLength());

src/main/java/org/htmlunit/javascript/host/crypto/SubtleCrypto.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@
4747
import javax.crypto.spec.SecretKeySpec;
4848

4949
import org.htmlunit.corejs.javascript.EcmaError;
50-
import org.htmlunit.corejs.javascript.NativeObject;
5150
import org.htmlunit.corejs.javascript.NativePromise;
52-
import org.htmlunit.corejs.javascript.ScriptRuntime;
5351
import org.htmlunit.corejs.javascript.Scriptable;
5452
import org.htmlunit.corejs.javascript.ScriptableObject;
55-
import org.htmlunit.corejs.javascript.TopLevel;
5653
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
5754
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBufferView;
5855
import org.htmlunit.javascript.HtmlUnitScriptable;
@@ -620,8 +617,7 @@ private Scriptable createKeyPair(final KeyPair keyPair, final Scriptable algoObj
620617
final CryptoKey privateKey = CryptoKey.create(
621618
getParentScope(), keyPair.getPrivate(), isExtractable, algoObj, privateUsages);
622619

623-
final NativeObject keyPairObj = new NativeObject();
624-
ScriptRuntime.setBuiltinProtoAndParent(keyPairObj, scope, TopLevel.Builtins.Object);
620+
final Scriptable keyPairObj = JavaScriptEngine.newObject(scope);
625621
ScriptableObject.putProperty(keyPairObj, "publicKey", publicKey);
626622
ScriptableObject.putProperty(keyPairObj, "privateKey", privateKey);
627623
return keyPairObj;
@@ -917,12 +913,12 @@ NativeArrayBuffer createArrayBuffer(final byte[] data) {
917913
* @throws IllegalArgumentException if usages array is invalid or contains unrecognized values
918914
*/
919915
static List<String> resolveKeyUsages(final String algorithm, final Scriptable keyUsages) {
920-
if (!ScriptRuntime.isArrayLike(keyUsages)) {
916+
if (!JavaScriptEngine.isArrayLike(keyUsages)) {
921917
throw new IllegalArgumentException("An invalid or illegal string was specified");
922918
}
923919

924920
final Set<String> supportedKeyUsages = new HashSet<>();
925-
for (final Object usage : ScriptRuntime.getArrayElements(keyUsages)) {
921+
JavaScriptEngine.iterateArrayLike(null, keyUsages, usage -> {
926922
if (!(usage instanceof String usageStr)) {
927923
throw new IllegalArgumentException("An invalid or illegal string was specified");
928924
}
@@ -934,7 +930,7 @@ static List<String> resolveKeyUsages(final String algorithm, final Scriptable ke
934930
if (supportedAlgorithms != null && supportedAlgorithms.contains(algorithm)) {
935931
supportedKeyUsages.add(usageStr);
936932
}
937-
}
933+
});
938934

939935
// maintain canonical ordering per RECOGNIZED_KEY_USAGES
940936
final List<String> sortedKeyUsages = new ArrayList<>();

src/main/java/org/htmlunit/javascript/host/intl/Intl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ public static Object getCanonicalLocales(final Context cx, final Scriptable this
149149
}
150150
else if (localesArgument instanceof Scriptable scriptable) {
151151
if (JavaScriptEngine.isArrayLike(scriptable)) {
152-
final long len = JavaScriptEngine.lengthOfArrayLike(cx, scriptable);
153-
for (int i = 0; i < len; i++) {
154-
final Object elem = scriptable.get(i, scriptable);
152+
JavaScriptEngine.iterateArrayLike(cx, scriptable, elem -> {
155153
if (elem instanceof String s) {
156154
languageTags.add(s);
157155
}
@@ -161,7 +159,7 @@ else if (elem instanceof ScriptableObject) {
161159
else {
162160
throw JavaScriptEngine.typeError("Invalid element in locales argument");
163161
}
164-
}
162+
});
165163
}
166164
else {
167165
languageTags.add(JavaScriptEngine.toString(localesArgument));

0 commit comments

Comments
 (0)