Skip to content

Commit

Permalink
more concrete return values
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 10, 2024
1 parent 8a9d176 commit 3bbdb15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 36 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/htmlunit/javascript/HtmlUnitScriptable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.corejs.javascript.LambdaConstructor;
import org.htmlunit.corejs.javascript.LambdaFunction;
import org.htmlunit.corejs.javascript.NativePromise;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.ScriptableObject;
import org.htmlunit.html.DomNode;
Expand Down Expand Up @@ -419,24 +420,24 @@ public HtmlUnitScriptable clone() {
}
}

protected Object setupPromise(final FailableSupplier<Object, IOException> resolver) {
protected NativePromise setupPromise(final FailableSupplier<Object, IOException> resolver) {
final Scriptable scope = ScriptableObject.getTopLevelScope(this);
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");

try {
final LambdaFunction resolve = (LambdaFunction) getProperty(ctor, "resolve");
return resolve.call(Context.getCurrentContext(), this, ctor, new Object[] {resolver.get()});
return (NativePromise) resolve.call(Context.getCurrentContext(), this, ctor, new Object[] {resolver.get()});
}
catch (final IOException e) {
final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
return reject.call(Context.getCurrentContext(), this, ctor, new Object[] {e.getMessage()});
return (NativePromise) reject.call(Context.getCurrentContext(), this, ctor, new Object[] {e.getMessage()});
}
}

protected Object setupRejectedPromise(final Supplier<Object> resolver) {
protected NativePromise setupRejectedPromise(final Supplier<Object> resolver) {
final Scriptable scope = ScriptableObject.getTopLevelScope(this);
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
return reject.call(Context.getCurrentContext(), this, ctor, new Object[] {resolver.get()});
return (NativePromise) reject.call(Context.getCurrentContext(), this, ctor, new Object[] {resolver.get()});
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/htmlunit/javascript/host/FontFaceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;

import org.htmlunit.corejs.javascript.NativePromise;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxFunction;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void jsConstructor() {
* when all the fonts are loaded; it is rejected if one of the fonts failed to load.
*/
@JsxFunction
public Object load(final String font, final String text) {
public NativePromise load(final String font, final String text) {
return setupPromise(() -> "");
}
}
27 changes: 14 additions & 13 deletions src/main/java/org/htmlunit/javascript/host/crypto/SubtleCrypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.htmlunit.javascript.host.crypto;

import org.htmlunit.corejs.javascript.NativePromise;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
Expand All @@ -39,7 +40,7 @@ public void jsConstructor() {
throw JavaScriptEngine.reportRuntimeError("Illegal constructor.");
}

private Object notImplemented() {
private NativePromise notImplemented() {
return setupRejectedPromise(() ->
new DOMException("Operation is not supported", DOMException.NOT_SUPPORTED_ERR));
}
Expand All @@ -50,7 +51,7 @@ private Object notImplemented() {
* @return a Promise which will be fulfilled with the encrypted data (also known as "ciphertext")
*/
@JsxFunction
public Object encrypt() {
public NativePromise encrypt() {
return notImplemented();
}

Expand All @@ -60,7 +61,7 @@ public Object encrypt() {
* @return a Promise which will be fulfilled with the decrypted data (also known as "plaintext")
*/
@JsxFunction
public Object decrypt() {
public NativePromise decrypt() {
return notImplemented();
}

Expand All @@ -70,7 +71,7 @@ public Object decrypt() {
* @return a Promise which will be fulfilled with the signature
*/
@JsxFunction
public Object sign() {
public NativePromise sign() {
return notImplemented();
}

Expand All @@ -80,7 +81,7 @@ public Object sign() {
* @return a Promise which will be fulfilled with a boolean value indicating whether the signature is valid
*/
@JsxFunction
public Object verify() {
public NativePromise verify() {
return notImplemented();
}

Expand All @@ -90,7 +91,7 @@ public Object verify() {
* @return a Promise which will be fulfilled with the digest
*/
@JsxFunction
public Object digest() {
public NativePromise digest() {
return notImplemented();
}

Expand All @@ -100,7 +101,7 @@ public Object digest() {
* @return a new key (for symmetric algorithms) or key pair (for public-key algorithms)
*/
@JsxFunction
public Object generateKey() {
public NativePromise generateKey() {
return notImplemented();
}

Expand All @@ -110,7 +111,7 @@ public Object generateKey() {
* @return a Promise which will be fulfilled with a CryptoKey object representing the new key
*/
@JsxFunction
public Object deriveKey() {
public NativePromise deriveKey() {
return notImplemented();
}

Expand All @@ -120,7 +121,7 @@ public Object deriveKey() {
* @return a Promise which will be fulfilled with an ArrayBuffer containing the derived bits
*/
@JsxFunction
public Object deriveBits() {
public NativePromise deriveBits() {
return notImplemented();
}

Expand All @@ -130,7 +131,7 @@ public Object deriveBits() {
* @return a CryptoKey object that you can use in the Web Crypto API
*/
@JsxFunction
public Object importKey() {
public NativePromise importKey() {
return notImplemented();
}

Expand All @@ -140,7 +141,7 @@ public Object importKey() {
* @return the key in an external, portable format
*/
@JsxFunction
public Object exportKey() {
public NativePromise exportKey() {
return notImplemented();
}

Expand All @@ -150,7 +151,7 @@ public Object exportKey() {
* @return a Promise that fulfills with an ArrayBuffer containing the encrypted exported key
*/
@JsxFunction
public Object wrapKey() {
public NativePromise wrapKey() {
return notImplemented();
}

Expand All @@ -160,7 +161,7 @@ public Object wrapKey() {
* @return a Promise that fulfills with the unwrapped key as a CryptoKey object
*/
@JsxFunction
public Object unwrapKey() {
public NativePromise unwrapKey() {
return notImplemented();
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/htmlunit/javascript/host/file/Blob.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.htmlunit.HttpHeader;
import org.htmlunit.WebRequest;
import org.htmlunit.corejs.javascript.NativeArray;
import org.htmlunit.corejs.javascript.NativePromise;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.ScriptableObject;
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
Expand Down Expand Up @@ -337,7 +338,7 @@ public String getType() {
* data in binary form.
*/
@JsxFunction
public Object arrayBuffer() {
public NativePromise arrayBuffer() {
return setupPromise(() -> {
final byte[] bytes = getBytes();
final NativeArrayBuffer buffer = new NativeArrayBuffer(bytes.length);
Expand Down Expand Up @@ -397,7 +398,7 @@ public ReadableStream stream() {
* contents of the blob, interpreted as UTF-8.
*/
@JsxFunction
public Object text() {
public NativePromise text() {
return setupPromise(() -> getBackend().getText());
}

Expand Down
15 changes: 0 additions & 15 deletions src/test/java/org/htmlunit/archunit/ArchitectureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,25 +272,12 @@ public boolean test(final JavaClass javaClass) {
.areAnnotatedWith(JsxFunction.class)
.and().doNotHaveFullName("org.htmlunit.javascript.host.Element.insertAdjacentElement(java.lang.String, java.lang.Object)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.External.isSearchProviderInstalled()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.FontFaceSet.load(java.lang.String, java.lang.String)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.SimpleArray.item(int)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.SimpleArray.namedItem(java.lang.String)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.Storage.getItem(java.lang.String)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.Window.setInterval(org.htmlunit.corejs.javascript.Context, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.Scriptable, [Ljava.lang.Object;, org.htmlunit.corejs.javascript.Function)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.Window.setTimeout(org.htmlunit.corejs.javascript.Context, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.Scriptable, [Ljava.lang.Object;, org.htmlunit.corejs.javascript.Function)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.canvas.IntersectionObserver.takeRecords()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.decrypt()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.deriveBits()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.deriveKey()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.digest()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.encrypt()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.exportKey()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.generateKey()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.importKey()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.sign()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.unwrapKey()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.verify()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.crypto.SubtleCrypto.wrapKey()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.css.CSSRuleList.item(int)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.css.StyleSheetList.item(int)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.dom.DOMTokenList.item(int)")
Expand All @@ -315,8 +302,6 @@ public boolean test(final JavaClass javaClass) {
.and().doNotHaveFullName("org.htmlunit.javascript.host.dom.Range.cloneRange()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.dom.Range.compareBoundaryPoints(int, org.htmlunit.javascript.host.dom.Range)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.dom.Text.splitText(int)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.file.Blob.arrayBuffer()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.file.Blob.text()")
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLCanvasElement.getContext(java.lang.String)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLCollection.item(java.lang.Object)")
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLCollection.namedItem(java.lang.String)")
Expand Down

0 comments on commit 3bbdb15

Please sign in to comment.