@live-codes/pascal-wasm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +501 -0
- package/README.md +278 -0
- package/THIRD-PARTY-NOTICES.md +48 -0
- package/assets/pas2js.wasm +0 -0
- package/assets/rtl/Rtl.BrowserLoadHelper.pas +179 -0
- package/assets/rtl/browserconsole.pas +190 -0
- package/assets/rtl/classes.pas +11371 -0
- package/assets/rtl/js.pas +2211 -0
- package/assets/rtl/manifest.json +19 -0
- package/assets/rtl/math.pas +903 -0
- package/assets/rtl/p2jsres.pp +334 -0
- package/assets/rtl/rtl.js +1563 -0
- package/assets/rtl/rtlconsts.pas +97 -0
- package/assets/rtl/simplelinkedlist.pas +152 -0
- package/assets/rtl/system.pas +1166 -0
- package/assets/rtl/sysutils.pas +8959 -0
- package/assets/rtl/types.pas +2296 -0
- package/assets/rtl/typinfo.pas +1680 -0
- package/assets/rtl/web.pas +3586 -0
- package/assets/rtl/weborworker.pas +2101 -0
- package/dist/pascal-wasm.iife.min.js +6 -0
- package/dist/pascal-wasm.iife.min.js.map +7 -0
- package/package.json +54 -0
- package/src/assets.js +119 -0
- package/src/compiler.js +142 -0
- package/src/config.js +24 -0
- package/src/iife.js +27 -0
- package/src/index.js +130 -0
- package/src/vendor/browser_wasi_shim/debug.js +1 -0
- package/src/vendor/browser_wasi_shim/fd.js +1 -0
- package/src/vendor/browser_wasi_shim/fs_mem.js +1 -0
- package/src/vendor/browser_wasi_shim/fs_opfs.js +1 -0
- package/src/vendor/browser_wasi_shim/index.js +1 -0
- package/src/vendor/browser_wasi_shim/strace.js +1 -0
- package/src/vendor/browser_wasi_shim/wasi.js +1 -0
- package/src/vendor/browser_wasi_shim/wasi_defs.js +1 -0
- package/types/index.d.ts +89 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
{
|
|
2
|
+
This file is part of the Pas2JS run time library.
|
|
3
|
+
Copyright (c) 2023 by Michael Van Canneyt
|
|
4
|
+
|
|
5
|
+
Low-level Pas2JS resource handling.
|
|
6
|
+
|
|
7
|
+
See the file COPYING.FPC, included in this distribution,
|
|
8
|
+
for details about the copyright.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
|
|
14
|
+
**********************************************************************}
|
|
15
|
+
{$IFNDEF FPC_DOTTEDUNITS}
|
|
16
|
+
unit p2jsres;
|
|
17
|
+
{$ENDIF}
|
|
18
|
+
|
|
19
|
+
{$mode objfpc}
|
|
20
|
+
{$h+}
|
|
21
|
+
{$modeswitch externalclass}
|
|
22
|
+
|
|
23
|
+
interface
|
|
24
|
+
|
|
25
|
+
{$IFDEF FPC_DOTTEDUNITS}
|
|
26
|
+
uses System.Types;
|
|
27
|
+
{$ELSE}
|
|
28
|
+
uses types;
|
|
29
|
+
{$ENDIF}
|
|
30
|
+
|
|
31
|
+
Type
|
|
32
|
+
TResourceSource = (rsJS,rsHTML);
|
|
33
|
+
TResourceInfo = record
|
|
34
|
+
name : string;
|
|
35
|
+
encoding : string;
|
|
36
|
+
resourceunit : string;
|
|
37
|
+
format : string;
|
|
38
|
+
data : string;
|
|
39
|
+
end;
|
|
40
|
+
TResourceEnumCallBack = Reference to function(const resName : string) : boolean;
|
|
41
|
+
TResourcesLoadedEnumCallBack = Reference to Procedure(const LoadedResources : Array of String);
|
|
42
|
+
TResourcesLoadErrorCallBack = Reference to Procedure(const aError : string);
|
|
43
|
+
|
|
44
|
+
Function SetResourceSource(aSource : TResourceSource) : TResourceSource;
|
|
45
|
+
|
|
46
|
+
Function GetResourceNames : TStringDynArray;
|
|
47
|
+
Function GetResourceNames(aSource : TResourceSource) : TStringDynArray;
|
|
48
|
+
|
|
49
|
+
Function EnumResources(aCallback : TResourceEnumCallBack) : Integer;
|
|
50
|
+
Function EnumResources(aSource : TResourceSource; aCallback : TResourceEnumCallBack) : Integer;
|
|
51
|
+
|
|
52
|
+
Function GetResourceInfo(Const aName : String; var aInfo : TResourceInfo) : Boolean;
|
|
53
|
+
Function GetResourceInfo(aSource : TResourceSource; Const aName : String; var aInfo : TResourceInfo) : Boolean;
|
|
54
|
+
|
|
55
|
+
Procedure LoadHTMLLinkResources(const aURL : String; OnLoad : TResourcesLoadedEnumCallBack = Nil; OnError : TResourcesLoadErrorCallBack = Nil);
|
|
56
|
+
|
|
57
|
+
implementation
|
|
58
|
+
|
|
59
|
+
Uses
|
|
60
|
+
{$IFDEF FPC_DOTTEDUNITS}
|
|
61
|
+
System.SysUtils, JSApi.JS, BrowserApi.Web;
|
|
62
|
+
{$ELSE}
|
|
63
|
+
sysutils, js, web;
|
|
64
|
+
{$ENDIF}
|
|
65
|
+
|
|
66
|
+
var
|
|
67
|
+
gMode: TResourceSource;
|
|
68
|
+
|
|
69
|
+
{ ---------------------------------------------------------------------
|
|
70
|
+
Global entry points
|
|
71
|
+
---------------------------------------------------------------------}
|
|
72
|
+
|
|
73
|
+
Function SeTResourceSource(aSource : TResourceSource) : TResourceSource;
|
|
74
|
+
begin
|
|
75
|
+
Result:=gMode;
|
|
76
|
+
gMode:=aSource;
|
|
77
|
+
end;
|
|
78
|
+
|
|
79
|
+
Function GetResourceNames : TStringDynArray;
|
|
80
|
+
begin
|
|
81
|
+
Result:=GetResourceNames(gMode);
|
|
82
|
+
end;
|
|
83
|
+
|
|
84
|
+
Function EnumResources(aCallback : TResourceEnumCallBack) : Integer;
|
|
85
|
+
|
|
86
|
+
begin
|
|
87
|
+
Result:=EnumResources(gMode,aCallback);
|
|
88
|
+
end;
|
|
89
|
+
|
|
90
|
+
Function GetResourceInfo(Const aName : String; var aInfo : TResourceInfo) : Boolean;
|
|
91
|
+
|
|
92
|
+
begin
|
|
93
|
+
Result:=GetResourceInfo(gMode,aName,aInfo);
|
|
94
|
+
end;
|
|
95
|
+
|
|
96
|
+
{ ---------------------------------------------------------------------
|
|
97
|
+
JS resources
|
|
98
|
+
---------------------------------------------------------------------}
|
|
99
|
+
|
|
100
|
+
Type
|
|
101
|
+
TRTLResourceInfo = class external name 'Object' (TJSObject)
|
|
102
|
+
name : string;
|
|
103
|
+
encoding : string;
|
|
104
|
+
resourceunit : string; external name 'unit';
|
|
105
|
+
format : string;
|
|
106
|
+
data : string;
|
|
107
|
+
end;
|
|
108
|
+
|
|
109
|
+
function rtlGetResourceList : TStringDynArray; external name 'rtl.getResourceList';
|
|
110
|
+
function rtlGetResource(const aName : string) : TRTLResourceInfo; external name 'rtl.getResource';
|
|
111
|
+
|
|
112
|
+
Function GetRTLResourceInfo(Const aName : String; var aInfo : TResourceInfo) : Boolean;
|
|
113
|
+
|
|
114
|
+
Var
|
|
115
|
+
RTLInfo : TRTLResourceInfo;
|
|
116
|
+
|
|
117
|
+
begin
|
|
118
|
+
RTLInfo:=rtlGetResource(lowercase(aName));
|
|
119
|
+
Result:=Assigned(RTLInfo);
|
|
120
|
+
if Result then
|
|
121
|
+
begin
|
|
122
|
+
aInfo.name:=RTLinfo.name;
|
|
123
|
+
aInfo.encoding:=RTLinfo.encoding;
|
|
124
|
+
aInfo.format:=RTLinfo.format;
|
|
125
|
+
aInfo.resourceunit:=RTLinfo.resourceunit;
|
|
126
|
+
aInfo.data:=RTLinfo.data;
|
|
127
|
+
end;
|
|
128
|
+
end;
|
|
129
|
+
|
|
130
|
+
{ ---------------------------------------------------------------------
|
|
131
|
+
HTML resources
|
|
132
|
+
---------------------------------------------------------------------}
|
|
133
|
+
|
|
134
|
+
Const
|
|
135
|
+
IDPrefix = 'resource-';
|
|
136
|
+
|
|
137
|
+
Function IsResourceLink(L : TJSHTMLLinkElement) : Boolean;
|
|
138
|
+
|
|
139
|
+
begin
|
|
140
|
+
Result:=(Copy(L.id,1,Length(IDPrefix))=IDPrefix) and (isDefined(L.Dataset['unit'])) and (Copy(L.href,1,4)='data')
|
|
141
|
+
end;
|
|
142
|
+
|
|
143
|
+
Function GetHTMLResources : TStringDynArray;
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
Var
|
|
147
|
+
LC : TJSHTMLCollection;
|
|
148
|
+
L : TJSHTMLLinkElement;
|
|
149
|
+
I : Integer;
|
|
150
|
+
ID : String;
|
|
151
|
+
|
|
152
|
+
begin
|
|
153
|
+
SetLength(Result,0);
|
|
154
|
+
if not isDefined(document) then // If called in Node...
|
|
155
|
+
exit;
|
|
156
|
+
// No cache, we do this dynamically: it's possible to add link nodes at runtime.
|
|
157
|
+
LC:=document.getElementsByTagName('link');
|
|
158
|
+
For I:=0 to LC.length-1 do
|
|
159
|
+
begin
|
|
160
|
+
L:=TJSHTMLLinkElement(LC[i]);
|
|
161
|
+
ID:=L.ID;
|
|
162
|
+
if IsResourceLink(L) then
|
|
163
|
+
begin
|
|
164
|
+
Delete(ID,1,Length(IDPrefix));
|
|
165
|
+
if (ID<>'') then
|
|
166
|
+
TJSArray(Result).Push(ID);
|
|
167
|
+
end;
|
|
168
|
+
end;
|
|
169
|
+
end;
|
|
170
|
+
|
|
171
|
+
Function GetHTMLResourceInfo(Const aName : String; var aInfo : TResourceInfo) : Boolean;
|
|
172
|
+
|
|
173
|
+
Var
|
|
174
|
+
el : TJSElement;
|
|
175
|
+
L : TJSHTMLLinkElement absolute el;
|
|
176
|
+
S : String;
|
|
177
|
+
I : Integer;
|
|
178
|
+
|
|
179
|
+
begin
|
|
180
|
+
Result:=False;
|
|
181
|
+
if not isDefined(document) then // If called in Node...
|
|
182
|
+
exit;
|
|
183
|
+
El:=document.getElementByID(IDPrefix+lowercase(aName));
|
|
184
|
+
Result:=assigned(el) and SameText(el.tagName,'link');
|
|
185
|
+
if not Result then
|
|
186
|
+
exit;
|
|
187
|
+
ainfo.name:=lowercase(aName);
|
|
188
|
+
ainfo.Resourceunit:=String(L.Dataset['unit']);
|
|
189
|
+
S:=L.href;
|
|
190
|
+
S:=Copy(S,6,Length(S)-5); // strip data;
|
|
191
|
+
I:=Pos(',',S);
|
|
192
|
+
aInfo.data:=Copy(S,I+1,Length(S)-1);
|
|
193
|
+
S:=copy(S,1,I-1);
|
|
194
|
+
I:=Pos(';',S);
|
|
195
|
+
if I=0 then
|
|
196
|
+
aInfo.encoding:=''
|
|
197
|
+
else
|
|
198
|
+
begin
|
|
199
|
+
aInfo.encoding:=Copy(S,I+1,Length(S)-1);
|
|
200
|
+
S:=Copy(S,1,I-1);
|
|
201
|
+
end;
|
|
202
|
+
aInfo.Format:=S;
|
|
203
|
+
end;
|
|
204
|
+
|
|
205
|
+
Function HasTemplate : Boolean;
|
|
206
|
+
|
|
207
|
+
begin
|
|
208
|
+
asm
|
|
209
|
+
return ('content' in document.createElement('template'))
|
|
210
|
+
end;
|
|
211
|
+
Result:=false;
|
|
212
|
+
end;
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
Procedure LoadHTMLLinkResources(const aURL : String; OnLoad : TResourcesLoadedEnumCallBack = Nil; OnError : TResourcesLoadErrorCallBack = Nil);
|
|
216
|
+
|
|
217
|
+
function FetchOK(Res : JSValue) : JSValue;
|
|
218
|
+
var
|
|
219
|
+
Response : TJSResponse absolute res;
|
|
220
|
+
begin
|
|
221
|
+
Result:=Nil;
|
|
222
|
+
if not Response.ok then
|
|
223
|
+
begin
|
|
224
|
+
if Assigned(OnError) then
|
|
225
|
+
Raise TJSError.New('HTTP Error for URL aURL, status = '+IntToStr(Response.status)+' : '+Response.statusText)
|
|
226
|
+
end
|
|
227
|
+
else
|
|
228
|
+
Result:=Response.Text();
|
|
229
|
+
end;
|
|
230
|
+
|
|
231
|
+
function BlobOK(Res : JSValue) : JSValue;
|
|
232
|
+
|
|
233
|
+
Var
|
|
234
|
+
aText : String absolute res;
|
|
235
|
+
ID : String;
|
|
236
|
+
Tmpl : TJSHTMLTemplateElement;
|
|
237
|
+
El : TJSHTMLElement;
|
|
238
|
+
L : TJSHTMLLinkElement absolute El;
|
|
239
|
+
Arr : TStringDynArray;
|
|
240
|
+
aParent : TJSHTMLElement;
|
|
241
|
+
|
|
242
|
+
begin
|
|
243
|
+
Result:=Nil;
|
|
244
|
+
aParent:=TJSHTMLElement(document.head);
|
|
245
|
+
if aParent=Nil then
|
|
246
|
+
aParent:=TJSHTMLElement(document.body);
|
|
247
|
+
SetLength(Arr,0);
|
|
248
|
+
Tmpl:=TJSHTMLTemplateElement(Document.createElement('template'));
|
|
249
|
+
Tmpl.innerhtml:=TJSString(aText).trim;
|
|
250
|
+
el:=TJSHTMLElement(Tmpl.Content.firstElementChild);
|
|
251
|
+
While El<>Nil do
|
|
252
|
+
begin
|
|
253
|
+
if SameText(El.tagName,'link') and IsResourceLink(L) then
|
|
254
|
+
begin
|
|
255
|
+
aParent.Append(TJSHTMLElement(document.importNode(l,true)));
|
|
256
|
+
ID:=L.ID;
|
|
257
|
+
Delete(ID,1,Length(IDPrefix));
|
|
258
|
+
if (ID<>'') then
|
|
259
|
+
TJSArray(Arr).Push(ID);
|
|
260
|
+
end;
|
|
261
|
+
el:=TJSHTMLElement(el.nextElementSibling);
|
|
262
|
+
end;
|
|
263
|
+
if Assigned(OnLoad) then
|
|
264
|
+
OnLoad(Arr);
|
|
265
|
+
end;
|
|
266
|
+
|
|
267
|
+
function DoError (aValue : JSValue) : JSValue;
|
|
268
|
+
|
|
269
|
+
Var
|
|
270
|
+
aErr : TJSError absolute aValue;
|
|
271
|
+
|
|
272
|
+
begin
|
|
273
|
+
Result:=Nil;
|
|
274
|
+
if Assigned(OnError) then
|
|
275
|
+
if aErr=Nil then
|
|
276
|
+
OnError('Error: ' + aErr.message)
|
|
277
|
+
end;
|
|
278
|
+
|
|
279
|
+
begin
|
|
280
|
+
if not HasTemplate then
|
|
281
|
+
begin
|
|
282
|
+
if Assigned(OnError) then
|
|
283
|
+
OnError('No template support in this browser')
|
|
284
|
+
end
|
|
285
|
+
else
|
|
286
|
+
window.fetch(aURL)._then(@FetchOK)._then(@BlobOK).catch(@doError);
|
|
287
|
+
end;
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
{ ---------------------------------------------------------------------
|
|
291
|
+
Global entries, specifying resource mode
|
|
292
|
+
---------------------------------------------------------------------}
|
|
293
|
+
|
|
294
|
+
Function GetResourceNames(aSource : TResourceSource) : TStringDynArray;
|
|
295
|
+
|
|
296
|
+
begin
|
|
297
|
+
case aSource of
|
|
298
|
+
rsJS : Result:=rtlGetResourceList;
|
|
299
|
+
rsHTML : Result:=GetHTMLResources;
|
|
300
|
+
end;
|
|
301
|
+
end;
|
|
302
|
+
|
|
303
|
+
Function EnumResources(aSource : TResourceSource; aCallback : TResourceEnumCallBack) : Integer;
|
|
304
|
+
|
|
305
|
+
Var
|
|
306
|
+
RL : TStringDynArray;
|
|
307
|
+
I : Integer;
|
|
308
|
+
ContinueEnum : Boolean;
|
|
309
|
+
|
|
310
|
+
begin
|
|
311
|
+
Result:=0;
|
|
312
|
+
RL:=GetResourceNames(aSource);
|
|
313
|
+
I:=0;
|
|
314
|
+
Result:=Length(RL);
|
|
315
|
+
ContinueEnum:=True;
|
|
316
|
+
While (I<Result) and ContinueEnum do
|
|
317
|
+
begin
|
|
318
|
+
ContinueEnum:=aCallBack(RL[i]);
|
|
319
|
+
Inc(I);
|
|
320
|
+
end;
|
|
321
|
+
end;
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
Function GetResourceInfo(aSource : TResourceSource; Const aName : String; var aInfo : TResourceInfo) : Boolean;
|
|
325
|
+
|
|
326
|
+
begin
|
|
327
|
+
case aSource of
|
|
328
|
+
rsJS : Result:=GetRTLResourceInfo(aName,aInfo);
|
|
329
|
+
rsHTML : Result:=GetHTMLResourceInfo(aName,aInfo);
|
|
330
|
+
end;
|
|
331
|
+
end;
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
end.
|