wasmjob: switched job_js using variants, added fetch

This commit is contained in:
mattias 2022-08-26 16:00:15 +02:00
parent 2960042f2f
commit 80a4c037a0
6 changed files with 984 additions and 415 deletions

View File

@ -214,8 +214,8 @@ type
function GetJSObjectID: TJOBObjectID;
function GetJSObjectCastSrc: IJSObject;
function GetPascalClassName: string;
function GetProperties(const PropName: String): TJOB_JSValue; virtual;
procedure SetProperties(const PropName: String; const AValue: TJOB_JSValue); virtual;
function GetProperties(const PropName: String): Variant; virtual;
procedure SetProperties(const PropName: String; const AValue: Variant); virtual;
// call a function
procedure InvokeJSNoResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeType = jiCall); virtual;
function InvokeJSBooleanResult(const aName: string; Const Args: Array of const; Invoke: TJOBInvokeType = jiCall): Boolean; virtual;
@ -254,8 +254,8 @@ type
function toLocaleString: UnicodeString; virtual; overload;
function toString: String; override; overload;
function toUString: UnicodeString; virtual; overload;
function valueOf: TJOB_JSValue; virtual; overload;
property Properties[const PropName: String]: TJOB_JSValue read GetProperties write SetProperties; default;
function valueOf: Variant; virtual; overload;
property Properties[const PropName: String]: Variant read GetProperties write SetProperties; default;
end;
{ TJSObject }
@ -285,8 +285,8 @@ type
function GetJSObjectID: TJOBObjectID;
function GetJSObjectCastSrc: IJSObject;
function GetPascalClassName: string;
function GetProperties(const PropName: String): TJOB_JSValue; virtual;
procedure SetProperties(const PropName: String; const AValue: TJOB_JSValue); virtual;
function GetProperties(const PropName: String): Variant; virtual;
procedure SetProperties(const PropName: String; const AValue: Variant); virtual;
function FetchString(Len: NativeInt): UnicodeString;
function InvokeJSNoResultFunc(const aName: string; Const Args: Array of const;
const InvokeFunc: TJOBInvokeNoResultFunc; Invoke: TJOBInvokeType): TJOBResult;
@ -349,8 +349,8 @@ type
function toLocaleString: UnicodeString; virtual; overload;
function toString: String; override; overload;
function toUString: UnicodeString; virtual; overload;
function valueOf: TJOB_JSValue; virtual; overload;
property Properties[const PropName: String]: TJOB_JSValue read GetProperties write SetProperties; default;
function valueOf: Variant; virtual; overload;
property Properties[const PropName: String]: Variant read GetProperties write SetProperties; default;
end;
{ IJSSet }
@ -835,7 +835,7 @@ type
class function Cast(Intf: IJSObject): IJSError; overload;
end;
TJSPromiseResolver = function(const aValue: TJOB_JSValue): TJOB_JSValue of object;
TJSPromiseResolver = function(const aValue: Variant): Variant of object;
TJSPromiseExecutor = procedure(const OnResolve, OnReject: TJSPromiseResolver) of object;
TJSPromiseFinallyHandler = procedure of object;
@ -843,20 +843,11 @@ type
IJSPromise = interface(IJSObject)
['{2BFE673B-B5D4-4F31-96CD-5E1A60EFBE26}']
function all(const arg: IJSArray): IJSPromise; overload;
function all(const arg: TJOB_JSValue): IJSPromise; overload;
function allSettled(const arg: IJSArray): IJSPromise; overload;
function allSettled(const arg: TJOB_JSValue): IJSPromise; overload;
function race(const arg: IJSArray): IJSPromise; overload;
function race(const arg: TJOB_JSValue): IJSPromise; overload;
function reject(const reason: UnicodeString): IJSPromise; overload;
function reject(const reason: TJOB_JSValue): IJSPromise; overload;
function resolve(const value: boolean): IJSPromise; overload;
function resolve(const value: longint): IJSPromise; overload;
function resolve(const value: double): IJSPromise; overload;
function resolve(const value: IJSObject): IJSPromise; overload;
function resolve(const value: UnicodeString): IJSPromise; overload;
function resolve(const value: TJOB_JSValue): IJSPromise; overload;
function all(const arg: Variant): IJSPromise; overload;
function allSettled(const arg: Variant): IJSPromise; overload;
function race(const arg: Variant): IJSPromise; overload;
function reject(const reason: Variant): IJSPromise; overload;
function resolve(const value: Variant): IJSPromise; overload;
function resolve: IJSPromise; overload;
function _then(const OnAccepted: TJSPromiseResolver): IJSPromise; overload;
function _then(const OnAccepted, OnRejected: TJSPromiseResolver) : IJSPromise; overload;
@ -869,20 +860,11 @@ type
TJSPromise = class(TJSObject,IJSPromise)
public
//class function Create(const Executor: TJSPromiseExecutor): IJSPromise; overload;
function all(const arg: IJSArray): IJSPromise; overload;
function all(const arg: TJOB_JSValue): IJSPromise; overload;
function allSettled(const arg: IJSArray): IJSPromise; overload;
function allSettled(const arg: TJOB_JSValue): IJSPromise; overload;
function race(const arg: IJSArray): IJSPromise; overload;
function race(const arg: TJOB_JSValue): IJSPromise; overload;
function reject(const reason: UnicodeString): IJSPromise; overload;
function reject(const reason: TJOB_JSValue): IJSPromise; overload;
function resolve(const value: boolean): IJSPromise; overload;
function resolve(const value: longint): IJSPromise; overload;
function resolve(const value: double): IJSPromise; overload;
function resolve(const value: IJSObject): IJSPromise; overload;
function resolve(const value: UnicodeString): IJSPromise; overload;
function resolve(const value: TJOB_JSValue): IJSPromise; overload;
function all(const arg: Variant): IJSPromise; overload;
function allSettled(const arg: Variant): IJSPromise; overload;
function race(const arg: Variant): IJSPromise; overload;
function reject(const reason: Variant): IJSPromise; overload;
function resolve(const value: Variant): IJSPromise; overload;
function resolve: IJSPromise; overload;
function _then(const OnAccepted: TJSPromiseResolver): IJSPromise; overload;
function _then(const OnAccepted, OnRejected: TJSPromiseResolver) : IJSPromise; overload;
@ -1151,14 +1133,10 @@ end;
function JOBCallTJSPromiseResolver(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
var
aValue: TJOB_JSValue;
aValue: Variant;
begin
aValue:=H.GetValue;
try
Result:=H.AllocJSValue(TJSPromiseResolver(aMethod)(aValue));
finally
aValue.Free;
end;
aValue:=H.GetVariant;
Result:=H.AllocVariant(TJSPromiseResolver(aMethod)(aValue));
end;
function JOBCallTJSPromiseFinallyHandler(const aMethod: TMethod; var H: TJOBCallbackHelper): PByte;
@ -1183,72 +1161,27 @@ end;
{ TJSPromise }
function TJSPromise.all(const arg: IJSArray): IJSPromise;
function TJSPromise.all(const arg: Variant): IJSPromise;
begin
Result:=InvokeJSObjectResult('all',[arg],TJSPromise) as IJSPromise;
end;
function TJSPromise.all(const arg: TJOB_JSValue): IJSPromise;
begin
Result:=InvokeJSObjectResult('all',[arg],TJSPromise) as IJSPromise;
end;
function TJSPromise.allSettled(const arg: IJSArray): IJSPromise;
function TJSPromise.allSettled(const arg: Variant): IJSPromise;
begin
Result:=InvokeJSObjectResult('allSettled',[arg],TJSPromise) as IJSPromise;
end;
function TJSPromise.allSettled(const arg: TJOB_JSValue): IJSPromise;
begin
Result:=InvokeJSObjectResult('allSettled',[arg],TJSPromise) as IJSPromise;
end;
function TJSPromise.race(const arg: IJSArray): IJSPromise;
function TJSPromise.race(const arg: Variant): IJSPromise;
begin
Result:=InvokeJSObjectResult('race',[arg],TJSPromise) as IJSPromise;
end;
function TJSPromise.race(const arg: TJOB_JSValue): IJSPromise;
begin
Result:=InvokeJSObjectResult('race',[arg],TJSPromise) as IJSPromise;
end;
function TJSPromise.reject(const reason: UnicodeString): IJSPromise;
function TJSPromise.reject(const reason: Variant): IJSPromise;
begin
Result:=InvokeJSObjectResult('reject',[reason],TJSPromise) as IJSPromise;
end;
function TJSPromise.reject(const reason: TJOB_JSValue): IJSPromise;
begin
Result:=InvokeJSObjectResult('reject',[reason],TJSPromise) as IJSPromise;
end;
function TJSPromise.resolve(const value: boolean): IJSPromise;
begin
Result:=InvokeJSObjectResult('resolve',[value],TJSPromise) as IJSPromise;
end;
function TJSPromise.resolve(const value: longint): IJSPromise;
begin
Result:=InvokeJSObjectResult('resolve',[value],TJSPromise) as IJSPromise;
end;
function TJSPromise.resolve(const value: double): IJSPromise;
begin
Result:=InvokeJSObjectResult('resolve',[value],TJSPromise) as IJSPromise;
end;
function TJSPromise.resolve(const value: IJSObject): IJSPromise;
begin
Result:=InvokeJSObjectResult('resolve',[value],TJSPromise) as IJSPromise;
end;
function TJSPromise.resolve(const value: UnicodeString): IJSPromise;
begin
Result:=InvokeJSObjectResult('resolve',[value],TJSPromise) as IJSPromise;
end;
function TJSPromise.resolve(const value: TJOB_JSValue): IJSPromise;
function TJSPromise.resolve(const value: Variant): IJSPromise;
begin
Result:=InvokeJSObjectResult('resolve',[value],TJSPromise) as IJSPromise;
end;
@ -2356,15 +2289,15 @@ begin
Result:=ClassName;
end;
function TJSObject.GetProperties(const PropName: String): TJOB_JSValue;
function TJSObject.GetProperties(const PropName: String): Variant;
begin
Result:=ReadJSPropertyValue(PropName);
Result:=ReadJSPropertyVariant(PropName);
end;
procedure TJSObject.SetProperties(const PropName: String;
const AValue: TJOB_JSValue);
const AValue: Variant);
begin
WriteJSPropertyValue(PropName,AValue);
WriteJSPropertyVariant(PropName,AValue);
end;
function TJSObject.FetchString(Len: NativeInt): UnicodeString;
@ -3268,9 +3201,9 @@ begin
Result:=InvokeJSUnicodeStringResult('toString',[]);
end;
function TJSObject.valueOf: TJOB_JSValue;
function TJSObject.valueOf: Variant;
begin
Result:=InvokeJSValueResult('valueOf',[]);
Result:=InvokeJSVariantResult('valueOf',[]);
end;
{ TJSDate }

File diff suppressed because it is too large Load Diff

View File

@ -667,10 +667,10 @@ partial interface Document {
[BinaryName="fullscreenEnabled", NeedsCallerType]
readonly attribute boolean mozFullScreenEnabled;
// Mattias: [NewObject]
// Promise<void> exitFullscreen();
// Mattias: [NewObject, BinaryName="exitFullscreen"]
// Promise<void> mozCancelFullScreen();
[NewObject]
Promise<void> exitFullscreen();
[NewObject, BinaryName="exitFullscreen"]
Promise<void> mozCancelFullScreen();
// Events handlers
attribute EventHandler onfullscreenchange;
@ -689,8 +689,8 @@ partial interface Document {
// Mozilla-internal document extensions specific to error pages.
partial interface Document {
// Mattias: [Func="Document::CallerIsTrustedAboutCertError", NewObject]
// Promise<any> addCertException(boolean isTemporary);
[Func="Document::CallerIsTrustedAboutCertError", NewObject]
Promise<any> addCertException(boolean isTemporary);
[Func="Document::CallerIsTrustedAboutHttpsOnlyError"]
void reloadWithHttpsOnlyException();
@ -818,9 +818,9 @@ partial interface Document {
[ChromeOnly] readonly attribute nsILoadGroup? documentLoadGroup;
// Blocks the initial document parser until the given promise is settled.
// Mattias: [ChromeOnly, NewObject]
// Promise<any> blockParsing(Promise<any> promise,
// optional BlockParsingOptions options = {});
[ChromeOnly, NewObject]
Promise<any> blockParsing(Promise<any> promise,
optional BlockParsingOptions options = {});
[Func="nsContentUtils::IsPDFJS", BinaryName="blockUnblockOnloadForPDFJS"]
void blockUnblockOnload(boolean block);
@ -910,18 +910,18 @@ partial interface Document {
// https://github.com/whatwg/html/issues/3338
partial interface Document {
// Mattias: [Pref="dom.storage_access.enabled", NewObject]
// Promise<boolean> hasStorageAccess();
// Mattias: [Pref="dom.storage_access.enabled", NewObject]
// Promise<void> requestStorageAccess();
[Pref="dom.storage_access.enabled", NewObject]
Promise<boolean> hasStorageAccess();
[Pref="dom.storage_access.enabled", NewObject]
Promise<void> requestStorageAccess();
};
// A privileged API to give chrome privileged code and the content script of the
// webcompat extension the ability to request the storage access for a given
// third party.
partial interface Document {
// Mattias: [Func="Document::CallerCanAccessPrivilegeSSA", NewObject]
// Promise<void> requestStorageAccessForOrigin(DOMString thirdPartyOrigin, optional boolean requireUserInteraction = true);
[Func="Document::CallerCanAccessPrivilegeSSA", NewObject]
Promise<void> requestStorageAccessForOrigin(DOMString thirdPartyOrigin, optional boolean requireUserInteraction = true);
};
enum DocumentAutoplayPolicy {
@ -1624,10 +1624,10 @@ Element includes ParentNode;
// https://fullscreen.spec.whatwg.org/#api
partial interface Element {
// Mattias: [NewObject, NeedsCallerType]
// Promise<void> requestFullscreen();
// Mattias: [NewObject, BinaryName="requestFullscreen", NeedsCallerType, Deprecated="MozRequestFullScreenDeprecatedPrefix"]
// Promise<void> mozRequestFullScreen();
[NewObject, NeedsCallerType]
Promise<void> requestFullscreen();
[NewObject, BinaryName="requestFullscreen", NeedsCallerType, Deprecated="MozRequestFullScreenDeprecatedPrefix"]
Promise<void> mozRequestFullScreen();
// Events handlers
attribute EventHandler onfullscreenchange;
@ -2838,8 +2838,8 @@ partial interface Window {
* @param {function} callback
* @returns {Promise}
*/
// Mattias: [NewObject, Func="nsGlobalWindowInner::IsPrivilegedChromeWindow"]
// Promise<any> promiseDocumentFlushed(PromiseDocumentFlushedCallback callback);
[NewObject, Func="nsGlobalWindowInner::IsPrivilegedChromeWindow"]
Promise<any> promiseDocumentFlushed(PromiseDocumentFlushedCallback callback);
[ChromeOnly]
readonly attribute boolean isChromeWindow;
@ -2871,7 +2871,7 @@ partial interface Window {
};
#endif
// Mattias: Window includes WindowOrWorkerGlobalScope;
Window includes WindowOrWorkerGlobalScope;
partial interface Window {
// Mattias: [Throws, Func="nsGlobalWindowInner::IsRequestIdleCallbackEnabled"]
@ -3190,7 +3190,7 @@ interface CustomElementRegistry {
[ChromeOnly, Throws]
void setElementCreationCallback(DOMString name, CustomElementCreationCallback callback);
any get(DOMString name);
// Mattias: [Throws]
// Mattias: [Throws]
// Promise<CustomElementConstructor> whenDefined(DOMString name);
[CEReactions] void upgrade(Node root);
};
@ -3565,8 +3565,8 @@ partial interface Navigator {
// https://wicg.github.io/web-share/#navigator-interface
partial interface Navigator {
// Mattias: [SecureContext, NewObject, Func="Navigator::HasShareSupport"]
// Promise<void> share(optional ShareData data = {});
[SecureContext, NewObject, Func="Navigator::HasShareSupport"]
Promise<void> share(optional ShareData data = {});
[SecureContext, Func="Navigator::HasShareSupport"]
boolean canShare(optional ShareData data = {});
};
@ -3957,16 +3957,16 @@ typedef sequence<ClipboardItem> ClipboardItems;
[SecureContext, Exposed=Window]
interface Clipboard : EventTarget {
// Mattias: [Pref="dom.events.asyncClipboard.clipboardItem", NewObject, NeedsSubjectPrincipal]
// Promise<ClipboardItems> read();
// Mattias: [Func="Clipboard::ReadTextEnabled", NewObject, NeedsSubjectPrincipal]
// Promise<DOMString> readText();
[Pref="dom.events.asyncClipboard.clipboardItem", NewObject, NeedsSubjectPrincipal]
Promise<ClipboardItems> read();
[Func="Clipboard::ReadTextEnabled", NewObject, NeedsSubjectPrincipal]
Promise<DOMString> readText();
// Mattias: [Pref="dom.events.asyncClipboard.clipboardItem", NewObject, NeedsSubjectPrincipal]
// Mattias: [Pref="dom.events.asyncClipboard.clipboardItem", NewObject, NeedsSubjectPrincipal]
// Promise<void> write(ClipboardItems data);
// Mattias: [NewObject, NeedsSubjectPrincipal]
// Promise<void> writeText(DOMString data);
[NewObject, NeedsSubjectPrincipal]
Promise<void> writeText(DOMString data);
};
partial interface Clipboard {
@ -4000,8 +4000,8 @@ interface ClipboardItem {
[Frozen, Cached, Pure]
readonly attribute sequence<DOMString> types;
// Mattias: [NewObject]
// Promise<Blob> getType(DOMString type);
[NewObject]
Promise<Blob> getType(DOMString type);
};
enum PresentationStyle { "unspecified", "inline", "attachment" };
@ -4157,8 +4157,8 @@ enum OrientationLockType {
[Exposed=Window]
interface ScreenOrientation : EventTarget {
// Mattias: [NewObject]
// Promise<void> lock(OrientationLockType orientation);
[NewObject]
Promise<void> lock(OrientationLockType orientation);
[Throws]
void unlock();
[Throws, NeedsCallerType]
@ -5332,8 +5332,8 @@ interface CSSStyleSheet : StyleSheet {
unsigned long insertRule(UTF8String rule, optional unsigned long index = 0);
[Throws, NeedsSubjectPrincipal]
void deleteRule(unsigned long index);
// Mattias: [NewObject, Pref="layout.css.constructable-stylesheets.enabled"]
// Promise<CSSStyleSheet> replace(UTF8String text);
[NewObject, Pref="layout.css.constructable-stylesheets.enabled"]
Promise<CSSStyleSheet> replace(UTF8String text);
[Throws, Pref="layout.css.constructable-stylesheets.enabled"]
void replaceSync(UTF8String text);
@ -6066,8 +6066,8 @@ interface HTMLImageElement : HTMLElement {
readonly attribute unsigned long naturalWidth;
readonly attribute unsigned long naturalHeight;
readonly attribute boolean complete;
// Mattias: [NewObject]
// Promise<void> decode();
[NewObject]
Promise<void> decode();
// Mattias: [NewObject, ChromeOnly]
// Promise<sequence<ImageText>> recognizeCurrentImageText();
};
@ -6752,8 +6752,8 @@ interface OffscreenCanvas : EventTarget {
[Throws]
ImageBitmap transferToImageBitmap();
// Mattias: [NewObject]
// Promise<Blob> convertToBlob(optional ImageEncodeOptions options = {});
[NewObject]
Promise<Blob> convertToBlob(optional ImageEncodeOptions options = {});
attribute EventHandler oncontextlost;
attribute EventHandler oncontextrestored;
@ -6798,8 +6798,8 @@ interface Blob {
// read from the Blob.
// Mattias: [NewObject, Throws] ReadableStream stream();
// Mattias: [NewObject] Promise<USVString> text();
// Mattias: [NewObject] Promise<ArrayBuffer> arrayBuffer();
[NewObject] Promise<USVString> text();
[NewObject] Promise<ArrayBuffer> arrayBuffer();
};
enum EndingType { "transparent", "native" };
@ -7539,8 +7539,8 @@ interface mixin WindowOrWorkerGlobalScope {
// https://fetch.spec.whatwg.org/#fetch-method
partial interface mixin WindowOrWorkerGlobalScope {
// Mattias: [NewObject, NeedsCallerType]
// Promise<Response> fetch(RequestInfo input, optional RequestInit init = {});
[NewObject, NeedsCallerType]
Promise<Response> fetch(RequestInfo input, optional RequestInit init = {});
};
// https://w3c.github.io/webappsec-secure-contexts/#monkey-patching-global-object
@ -7585,12 +7585,12 @@ interface CacheStorage {
// Mattias: [NewObject]
// Promise<Response> match(RequestInfo request, optional MultiCacheQueryOptions options = {});
// Mattias: [NewObject]
// Promise<boolean> has(DOMString cacheName);
// Mattias: [NewObject]
[NewObject]
Promise<boolean> has(DOMString cacheName);
// Mattias: [NewObject]
// Promise<Cache> open(DOMString cacheName);
// Mattias: [NewObject]
// Promise<boolean> delete(DOMString cacheName);
[NewObject]
Promise<boolean> delete(DOMString cacheName);
// Mattias: [NewObject]
// Promise<sequence<DOMString>> keys();
};
@ -7623,7 +7623,7 @@ interface Response {
[NewObject] static Response error();
[Throws,
NewObject] static Response redirect(USVString url, optional unsigned short status = 302);
NewObject] static Response redirect(USVString url, optional unsigned short status = 302);
readonly attribute ResponseType type;
@ -7641,6 +7641,8 @@ interface Response {
// For testing only.
[ChromeOnly] readonly attribute boolean hasCacheInfoChannel;
Promise<JSON> json();
};
// Mattias: Response includes Body;
@ -7694,3 +7696,83 @@ interface Headers {
// Note: Must be set prior to populating headers or will throw.
[ChromeOnly, SetterThrows] attribute HeadersGuardEnum guard;
};
/* -*- Mode: IDL; tab-width: 1; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* The origin of this IDL file is
* https://fetch.spec.whatwg.org/#request-class
*/
typedef (Request or USVString) RequestInfo;
typedef unsigned long nsContentPolicyType;
[Exposed=(Window,Worker)]
interface Request {
[Throws]
constructor(RequestInfo input, optional RequestInit init = {});
readonly attribute ByteString method;
readonly attribute USVString url;
[SameObject, BinaryName="headers_"] readonly attribute Headers headers;
readonly attribute RequestDestination destination;
readonly attribute USVString referrer;
[BinaryName="referrerPolicy_"]
readonly attribute ReferrerPolicy referrerPolicy;
readonly attribute RequestMode mode;
readonly attribute RequestCredentials credentials;
readonly attribute RequestCache cache;
readonly attribute RequestRedirect redirect;
readonly attribute DOMString integrity;
// If a main-thread fetch() promise rejects, the error passed will be a
// nsresult code.
[ChromeOnly]
readonly attribute boolean mozErrors;
// Mattias: [BinaryName="getOrCreateSignal"]
// readonly attribute AbortSignal signal;
[Throws,
NewObject] Request clone();
// Bug 1124638 - Allow chrome callers to set the context.
[ChromeOnly]
void overrideContentPolicyType(nsContentPolicyType context);
};
// Mattias: Request includes Body;
dictionary RequestInit {
ByteString method;
HeadersInit headers;
// Mattias: BodyInit? body;
USVString referrer;
ReferrerPolicy referrerPolicy;
RequestMode mode;
RequestCredentials credentials;
RequestCache cache;
RequestRedirect redirect;
DOMString integrity;
[ChromeOnly]
boolean mozErrors;
// Mattias: AbortSignal? signal;
// Mattias: [Pref="dom.fetchObserver.enabled"]
// ObserverCallback observe;
};
enum RequestDestination {
"",
"audio", "audioworklet", "document", "embed", "font", "frame", "iframe",
"image", "manifest", "object", "paintworklet", "report", "script",
"sharedworker", "style", "track", "video", "worker", "xslt"
};
enum RequestMode { "same-origin", "no-cors", "cors", "navigate" };
enum RequestCredentials { "omit", "same-origin", "include" };
enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" };
enum RequestRedirect { "follow", "error", "manual" };

View File

@ -85,9 +85,11 @@
-Jirtl.js
-Jc
-Jminclude
-dVerboseJOB"/>
<OtherDefines Count="1">
-dVerboseJOB
-dVerboseJOBCallback"/>
<OtherDefines Count="2">
<Define0 Value="VerboseJOB"/>
<Define1 Value="VerboseJOBCallback"/>
</OtherDefines>
<CompilerPath Value="$(pas2js)"/>
</Other>

View File

@ -5,7 +5,7 @@ library WasiFetch1;
{$codepage UTF8}
uses
SysUtils, JOB_Shared, JOB_Web, JOB_JS;
SysUtils, JOB_Shared, JOB_Web, JOB_JS, Variants;
type
@ -13,37 +13,30 @@ type
TWasmApp = class
private
function OnAccepted(const aValue: TJOB_JSValue): TJOB_JSValue;
function OnAccepted(const aValue: Variant): Variant;
function OnButtonClick(Event: IJSEvent): boolean;
function OnJSONFailed(const aValue: TJOB_JSValue): TJOB_JSValue;
function OnJSONReceived(const aValue: TJOB_JSValue): TJOB_JSValue;
function OnRejected(const aValue: TJOB_JSValue): TJOB_JSValue;
function OnJSONFailed(const aValue: Variant): Variant;
function OnJSONReceived(const aValue: Variant): Variant;
function OnRejected(const aValue: Variant): Variant;
public
procedure Run;
end;
{ TApplication }
function TWasmApp.OnAccepted(const aValue: TJOB_JSValue): TJOB_JSValue;
function TWasmApp.OnAccepted(const aValue: Variant): Variant;
var
Obj: IJSObject;
Response: IJSResponse;
p: IJSPromise;
begin
Result:=TJOB_Boolean.Create(false);
writeln('TWasmApp.OnAccepted ',aValue.AsString);
if aValue.Kind<>jjvkObject then
begin
writeln('TWasmApp.OnAccepted Expected object, but got '+JOB_JSValueKindNames[aValue.Kind]);
exit;
end;
Obj:=TJOB_Object(aValue).Value;
if Obj=nil then
begin
writeln('TWasmApp.OnAccepted Expected object, but got nil');
exit;
end;
Result:=false;
writeln('TWasmApp.OnAccepted START');
if not VarSupports(aValue,IJSObject,Obj) then
begin
writeln('TWasmApp.OnAccepted Expected object, but got '+VarTypeAsText(aValue));
exit;
end;
Response:=TJSResponse.Cast(Obj);
writeln('TWasmApp.OnAccepted Response: ok=',Response.ok);
writeln('TWasmApp.OnAccepted Response: status=',Response.status);
@ -51,58 +44,52 @@ begin
writeln('TWasmApp.OnAccepted Response: redirected=',Response.redirected);
writeln('TWasmApp.OnAccepted Response: URL="',Response.url,'"');
p:=Response.InvokeJSObjectResult('json',[],TJSPromise) as IJSPromise;
p._then(@OnJSONReceived,@OnJSONFailed);
Response.json._then(@OnJSONReceived,@OnJSONFailed);
TJOB_Boolean(Result).Value:=true;
Result:=true;
end;
function TWasmApp.OnButtonClick(Event: IJSEvent): boolean;
var
p: IJSPromise;
begin
writeln('TWasmApp.OnButtonClick ');
if Event=nil then ;
JSWindow.Alert('You triggered TWasmApp.OnButtonClick');
// JSWindow.Alert('You triggered TWasmApp.OnButtonClick');
p:=JSWindow.InvokeJSObjectResult('fetch',['Example.json'],TJSPromise) as IJSPromise;
p._then(@OnAccepted,@OnRejected);
//JSWindow.fetch('Example.json')._then(@OnAccepted,@OnRejected);
JSWindow.fetch('Example.json')._then(@OnAccepted,@OnRejected);
Result:=true;
end;
function TWasmApp.OnJSONFailed(const aValue: TJOB_JSValue): TJOB_JSValue;
function TWasmApp.OnJSONFailed(const aValue: Variant): Variant;
begin
Result:=TJOB_Boolean.Create(true);
writeln('TWasmApp.OnJSONFailed');
Result:=true;
end;
function TWasmApp.OnJSONReceived(const aValue: TJOB_JSValue): TJOB_JSValue;
function TWasmApp.OnJSONReceived(const aValue: Variant): Variant;
var
Obj: IJSObject;
begin
Result:=TJOB_Boolean.Create(true);
if aValue.Kind<>jjvkObject then
writeln('TWasmApp.OnJSONReceived START');
Result:=true;
if not VarSupports(aValue,IJSObject,Obj) then
begin
writeln('TWasmApp.OnJSONReceived Expected object, but got '+JOB_JSValueKindNames[aValue.Kind]);
writeln('TWasmApp.OnJSONReceived not an IJSObject');
writeln('TWasmApp.OnJSONReceived Expected object, but got '+VarTypeAsText(aValue));
exit;
end;
Obj:=TJOB_Object(aValue).Value;
if Obj=nil then
begin
writeln('TWasmApp.OnJSONReceived Expected object, but got nil');
exit;
end;
writeln('TWasmApp.OnJSONReceived Obj.name=',Obj.Properties['name'].AsString);
writeln('TWasmApp.OnJSONReceived Obj.value=',Obj.Properties['value'].AsString);
writeln('TWasmApp.OnJSONReceived Obj.name=',Obj.Properties['name']);
writeln('TWasmApp.OnJSONReceived Obj.value=',Obj.Properties['value']);
end;
function TWasmApp.OnRejected(const aValue: TJOB_JSValue): TJOB_JSValue;
function TWasmApp.OnRejected(const aValue: Variant): Variant;
begin
writeln('TWasmApp.OnRejected ',aValue.AsString);
Result:=TJOB_Boolean.Create(true);
writeln('TWasmApp.OnRejected START');
if VarIsStr(aValue) then
writeln('TWasmApp.OnRejected ',aValue)
else
writeln('TWasmApp.OnRejected ',VarTypeAsText(aValue));
Result:=true;
end;
procedure TWasmApp.Run;

View File

@ -189,6 +189,9 @@ begin
Result:=TJOBObjectID(NewId);
FLocalObjects[Result]:=Obj;
end;
{$IFDEF VerboseJOB}
writeln('TJSObjectBridge.RegisterLocalObject ',Result);
{$ENDIF}
end;
procedure TJSObjectBridge.SetWasiExports(const AValue: TWASIExports);
@ -212,7 +215,7 @@ var
fn: JSValue;
begin
{$IFDEF VerboseJOB}
writeln('TJOBBridge.Invoke_JSResult ObjId=',ObjId,' FuncNameP=',NameP,' FuncNameLen=',NameLen,' ArgsP=',ArgsP,' Invoke=',Invoke);
writeln('TJSObjectBridge.Invoke_JSResult ObjId=',ObjId,' FuncNameP=',NameP,' FuncNameLen=',NameLen,' ArgsP=',ArgsP,' Invoke=',Invoke);
{$ENDIF}
Obj:=FindObject(ObjId);
@ -221,10 +224,10 @@ begin
View:=getModuleMemoryDataView();
aBytes:=TJSUint8Array.New(View.buffer, NameP, NameLen);
//writeln('TJOBBridge.Invoke_JSResult aBytes=',aBytes);
//writeln('TJSObjectBridge.Invoke_JSResult aBytes=',aBytes);
PropName:=TypedArrayToString(aBytes);
{$IFDEF VerboseJOB}
writeln('TJOBBridge.Invoke_JSResult PropName="',PropName,'"');
writeln('TJSObjectBridge.Invoke_JSResult PropName="',PropName,'"');
{$ENDIF}
case Invoke of
@ -347,7 +350,7 @@ begin
exit(GetJOBResult(JSResult));
Result:=JOBResult_String;
FStringResult:=String(JSResult);
//writeln('TJOBBridge.Invoke_StringResult FStringResult="',FStringResult,'"');
//writeln('TJSObjectBridge.Invoke_StringResult FStringResult="',FStringResult,'"');
// set result length
getModuleMemoryDataView().setInt32(ResultP, length(FStringResult), env.IsLittleEndian);
@ -445,7 +448,7 @@ end;
function TJSObjectBridge.ReleaseObject(ObjId: TJOBObjectID): TJOBResult;
begin
{$IFDEF VerboseJOB}
writeln('TJOBBridge.ReleaseObject ',ObjId);
writeln('TJSObjectBridge.ReleaseObject ',ObjId);
{$ENDIF}
if ObjId<0 then
raise EJOBBridge.Create('cannot release a global object');
@ -505,16 +508,27 @@ var
Args, ResultP: TWasmNativeInt;
TempObjIds: TJOBObjectIDArray;
begin
//writeln('TJSObjectBridge called JS Method Call=',aCall,' Data=',aData,' Code=',aCode,' Args=',JSArguments.length);
{$IFDEF VerboseJOBCallback}
writeln('TJSObjectBridge Callback: JS Method Call=',aCall,' Data=',aData,' Code=',aCode,' Args=',JSArguments.length,' converting args for wasm...');
{$ENDIF}
Args:=CreateCallbackArgs(View,JSArguments,TempObjIds);
try
{$IFDEF VerboseJOBCallback}
writeln('TJSObjectBridge Callback: calling Wasm...');
{$ENDIF}
ResultP:=CallbackHandler(aCall,aData,aCode,Args); // this frees Args, and may detach View
View:=getModuleMemoryDataView();
//writeln('TJSObjectBridge called Wasm Call=',aCall,' Data=',aData,' Code=',aCode,' ResultP=',ResultP);
{$IFDEF VerboseJOBCallback}
writeln('TJSObjectBridge Callback: called Wasm Call=',aCall,' Data=',aData,' Code=',aCode,' ResultP=',ResultP,' getting Result...');
{$ENDIF}
Result:=EatCallbackResult(View,ResultP); // this frees ResultP
//writeln('TJSObjectBridge Result=',Result);
{$IFDEF VerboseJOBCallback}
writeln('TJSObjectBridge Callback: Result=',Result);
{$ENDIF}
finally
//writeln('After CallbackHandler: TempObjIds=',length(TempObjIds),' ',TempObjIds);
{$IFDEF VerboseJOBCallback}
writeln('TJSObjectBridge Callback: cleaning up TempObjIds=',length(TempObjIds),' ',TempObjIds);
{$ENDIF}
for i:=0 to length(TempObjIds)-1 do
ReleaseObject(TempObjIds[i]);
end;
@ -659,7 +673,7 @@ begin
for i:=0 to Cnt-1 do
begin
Result[i]:=ReadValue;
//writeln('TJOBBridge.GetInvokeArguments ',i,'/',Cnt,' = ',Result[i]);
//writeln('TJSObjectBridge.GetInvokeArguments ',i,'/',Cnt,' = ',Result[i]);
end;
end;