js.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2017-2020 by the Pas2JS development team.
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit JS;
  11. {$mode objfpc}
  12. {$modeswitch externalclass}
  13. interface
  14. uses
  15. Types;
  16. type
  17. // We cannot use EConvertError or Exception, this would result in a circular dependency.
  18. TJSArray = class;
  19. TJSMap = class;
  20. { EJS }
  21. EJS = class(TObject)
  22. private
  23. FMessage: string;
  24. Public
  25. constructor Create(const Msg: String); reintroduce;
  26. property Message : string Read FMessage Write FMessage;
  27. end;
  28. TJSObjectPropertyDescriptor = JSValue;
  29. Float32 = Double;
  30. Float64 = Double;
  31. { TJSObject }
  32. TJSObject = class external name 'Object'
  33. private
  34. function GetProperties(Name: String): JSValue; external name '[]';
  35. procedure SetProperties(Name: String; const AValue: JSValue); external name '[]';
  36. public
  37. constructor new;
  38. class function create(const proto: TJSObject): TJSObject;
  39. class function create(const proto, propertiesObject: TJSObject): TJSObject;
  40. class function assign(const Target, Source1: TJSObject): TJSObject; varargs;
  41. class procedure defineProperty(const obj: TJSObject; propname: String; const descriptor: TJSObjectPropertyDescriptor);
  42. //class procedure defineProperties
  43. class function freeze(const obj: TJSObject): TJSObject;
  44. class function getOwnPropertyDescriptor(const obj: TJSObject; propname: String): TJSObjectPropertyDescriptor;
  45. //class function getOwnPropertyDescriptors
  46. class function getOwnPropertyNames(const obj: TJSObject): TStringDynArray;
  47. class function values(const obj: JSValue): TJSObject;
  48. {$IFDEF FIREFOX}
  49. class function getOwnPropertySymbols(const obj: TJSObject): TJSValueDynArray;
  50. {$ENDIF}
  51. class function getPrototypeOf(const obj: TJSObject): TJSObject;
  52. {$IFDEF FIREFOX}
  53. class function _is(const value1, value2: JSValue): boolean;
  54. {$ENDIF}
  55. class function fromEntries(const obj: TJSObject): TJSObject;
  56. class function fromEntries(const obj: TJSArray): TJSObject;
  57. class function fromEntries(const obj: TJSMap): TJSObject;
  58. class function isExtensible(const obj: TJSObject): boolean;
  59. class function isFrozen(const obj: TJSObject): boolean;
  60. class function isSealed(const obj: TJSObject): boolean;
  61. class function keys(const obj: TJSObject): TStringDynArray;
  62. class function preventExtensions(const obj: TJSObject): TJSObject;
  63. class function seal(const obj: TJSObject): TJSObject;
  64. class function setPrototypeOf(const obj, prototype: TJSObject): TJSObject;
  65. function hasOwnProperty(prop: String): boolean;
  66. function isPrototypeOf(const obj: TJSObject): boolean;
  67. function propertyIsEnumerable(propname: String): boolean;
  68. function toLocaleString: String;
  69. function toString: String;
  70. function valueOf: JSValue;
  71. property Properties[Name: String]: JSValue read GetProperties write SetProperties; default;
  72. end;
  73. TJSObjectClass = class of TJSObject;
  74. TJSObjectDynArray = Array of TJSObject;
  75. TJSObjectDynArrayArray = Array of TJSObjectDynArray;
  76. TJSStringDynArray = Array of String;
  77. { TJSIteratorValue }
  78. TJSIteratorValue = class external name 'IteratorValue'
  79. public
  80. value : JSValue; external name 'value';
  81. done : boolean; external name 'done';
  82. end;
  83. { TJSIterator }
  84. TJSIterator = class external name 'Iterator'
  85. Public
  86. function next: TJSIteratorValue;
  87. end;
  88. TJSSet = class;
  89. TJSSetEventProc = reference to procedure(value : JSValue; key: NativeInt; set_: TJSSet);
  90. TJSSetProcCallBack = reference to procedure(value: JSValue; key: JSValue);
  91. { TJSSet }
  92. TJSSet = class external name 'Set'
  93. private
  94. FSize : NativeInt; external name 'size';
  95. public
  96. constructor new; overload;
  97. constructor new(aElement1 : JSValue); varargs; overload;
  98. function add(value: JSValue): TJSSet;
  99. function has(value: JSValue): Boolean;
  100. function delete(value: JSValue): Boolean;
  101. procedure clear;
  102. function values: TJSIterator;
  103. procedure forEach(const aCallBack: TJSSetEventProc); overload;
  104. procedure forEach(const aCallBack: TJSSetProcCallBack); overload;
  105. procedure forEach(const aCallBack: TJSSetEventProc; thisArg: JSValue); overload;
  106. function entries: TJSIterator;
  107. function keys: TJSIterator;
  108. Property size : NativeInt Read FSize;
  109. end;
  110. TJSMapFunctionCallBack = reference to function(arg : JSValue): JSValue;
  111. TJSMapProcCallBack = reference to procedure(value: JSValue; key: JSValue);
  112. { TJSMap }
  113. TJSMap = class external name 'Map'
  114. Private
  115. FSize : NativeInt; external name 'size';
  116. public
  117. constructor new; varargs; overload;
  118. constructor new(aElement1 : JSValue); varargs; overload;
  119. function &set(key: JSValue; value: JSValue) :TJSMap;
  120. function get(key : JSValue): JSValue;
  121. function has(key: JSValue): Boolean;
  122. function delete(key: JSValue): Boolean;
  123. procedure clear;
  124. function entries: TJSIterator;
  125. procedure forEach(const aCallBack: TJSMapFunctionCallBack); overload;
  126. procedure forEach(const aCallBack: TJSMapFunctionCallBack; thisArg: JSValue); overload;
  127. procedure forEach(const aCallBack: TJSMapProcCallBack); overload;
  128. function keys: TJSIterator;
  129. function values: TJSIterator;
  130. property size : NativeInt Read FSize;
  131. end;
  132. { TJSFunction }
  133. TJSFunction = class external name 'Function'(TJSObject)
  134. private
  135. Flength: NativeInt external name 'length';
  136. Fprototyp: TJSFunction external name 'prototyp';
  137. public
  138. name: String;
  139. property prototyp: TJSFunction read Fprototyp;
  140. property length: NativeInt read Flength;
  141. function apply(thisArg: TJSObject; const ArgArray: TJSValueDynArray): JSValue; varargs;
  142. function bind(thisArg: TJSObject): JSValue; varargs;
  143. function call(thisArg: TJSObject): JSValue; varargs;
  144. end;
  145. { TJSDate - wrapper for JavaScript Date }
  146. TJSDate = class external name 'Date'(TJSFunction)
  147. private
  148. function getDate: NativeInt;
  149. function getFullYear: NativeInt;
  150. function getHours: NativeInt;
  151. function getMilliseconds: NativeInt;
  152. function getMinutes: NativeInt;
  153. function getMonth: NativeInt;
  154. function getSeconds: NativeInt;
  155. function getYear: NativeInt;
  156. function getTime: NativeInt;
  157. function getUTCDate: NativeInt;
  158. function getUTCFullYear: NativeInt;
  159. function getUTCHours: NativeInt;
  160. function getUTCMilliseconds: NativeInt;
  161. function getUTCMinutes: NativeInt;
  162. function getUTCMonth: NativeInt;
  163. function getUTCSeconds: NativeInt;
  164. procedure setDate(const AValue: NativeInt);
  165. procedure setFullYear(const AValue: NativeInt);
  166. procedure setHours(const AValue: NativeInt);
  167. procedure setMilliseconds(const AValue: NativeInt);
  168. procedure setMinutes(const AValue: NativeInt);
  169. procedure setMonth(const AValue: NativeInt);
  170. procedure setSeconds(const AValue: NativeInt);
  171. procedure setYear(const AValue: NativeInt);
  172. procedure setTime(const AValue: NativeInt);
  173. procedure setUTCDate(const AValue: NativeInt);
  174. procedure setUTCFullYear(const AValue: NativeInt);
  175. procedure setUTCHours(const AValue: NativeInt);
  176. procedure setUTCMilliseconds(const AValue: NativeInt);
  177. procedure setUTCMinutes(const AValue: NativeInt);
  178. procedure setUTCMonth(const AValue: NativeInt);
  179. procedure setUTCSeconds(const AValue: NativeInt);
  180. public
  181. constructor New; reintroduce;
  182. constructor New(const MilliSecsSince1970: NativeInt); // milliseconds since 1 January 1970 00:00:00 UTC, with leap seconds ignored
  183. constructor New(const aDateString: String); // RFC 2822, ISO8601
  184. constructor New(aYear: NativeInt; aMonth: NativeInt; aDayOfMonth: NativeInt = 1;
  185. TheHours: NativeInt = 0; TheMinutes: NativeInt = 0; TheSeconds: NativeInt = 0;
  186. TheMilliseconds: NativeInt = 0);
  187. class function now: NativeInt; // current date and time in milliseconds since 1 January 1970 00:00:00 UTC, with leap seconds ignored
  188. class function parse(const aDateString: string): NativeInt; // format depends on browser
  189. class function UTC(aYear: NativeInt; aMonth: NativeInt = 0; aDayOfMonth: NativeInt = 1;
  190. TheHours: NativeInt = 0; TheMinutes: NativeInt = 0; TheSeconds: NativeInt = 0;
  191. TheMilliseconds: NativeInt = 0): NativeInt;
  192. function getDay: NativeInt;
  193. function getTimezoneOffset: NativeInt;
  194. function getUTCDay: NativeInt; // day of the week
  195. function toDateString: string; // human readable date, without time
  196. function toISOString: string; // ISO 8601 Extended Format
  197. function toJSON: string;
  198. function toGMTString: string; // in GMT timezone
  199. function toLocaleDateString: string; overload; // date in locale timezone, no time
  200. function toLocaleDateString(const aLocale : string) : string; overload; // date in locale timezone, no time
  201. function toLocaleDateString(const aLocale : string; aOptions : TJSObject) : string; overload; // date in locale timezone, no time
  202. function toLocaleString: string; reintroduce; // date and time in locale timezone
  203. function toLocaleTimeString: string; // time in locale timezone, no date
  204. function toTimeString: string; // time human readable, no date
  205. function toUTCString: string; // date and time using UTC timezone
  206. property Year: NativeInt read getYear write setYear;
  207. property Time: NativeInt read getTime write setTime; // milliseconds since 1 January 1970 00:00:00 UTC, with leap seconds ignored
  208. property FullYear: NativeInt read getFullYear write setFullYear;
  209. property UTCDate: NativeInt read getUTCDate write setUTCDate; // day of month
  210. property UTCFullYear: NativeInt read getUTCFullYear write setUTCFullYear;
  211. property UTCHours: NativeInt read getUTCHours write setUTCHours;
  212. property UTCMilliseconds: NativeInt read getUTCMilliseconds write setUTCMilliseconds;
  213. property UTCMinutes: NativeInt read getUTCMinutes write setUTCMinutes;
  214. property UTCMonth: NativeInt read getUTCMonth write setUTCMonth;
  215. property UTCSeconds: NativeInt read getUTCSeconds write setUTCSeconds;
  216. property Month: NativeInt read getMonth write setMonth;
  217. property Date: NativeInt read getDate write setDate; // day of the month, starting at 1
  218. property Hours: NativeInt read getHours write setHours;
  219. property Minutes: NativeInt read getMinutes write setMinutes;
  220. property Seconds: NativeInt read getSeconds write setSeconds;
  221. property Milliseconds: NativeInt read getMilliseconds write setMilliseconds;
  222. end;
  223. TLocaleCompareOptions = record
  224. localematched : string;
  225. usage: string;
  226. sensitivity : string;
  227. ignorePunctuation : Boolean;
  228. numeric : boolean;
  229. caseFirst : string;
  230. end;
  231. TJSRegexp = class external name 'RegExp'
  232. private
  233. {$IFDEF FIREFOX}
  234. // not on all browsers:
  235. FFlags : string; external name 'flags';
  236. FSticky : boolean; external name 'sticky';
  237. {$endif}
  238. fglobal: boolean; external name 'global';
  239. fignoreCase : boolean; external name 'ignoreCase';
  240. fmultiline : boolean; external name 'multiline';
  241. fsource : string; external name 'source';
  242. funicode : boolean; external name 'unicode';
  243. public
  244. Constructor New(Pattern : string);
  245. Constructor New(Pattern, Flags : string);
  246. lastIndex: NativeInt;
  247. function exec(aString : string): TStringDynArray;
  248. function test(aString : string) : boolean;
  249. function toString : String;
  250. property Global : boolean read fglobal;
  251. property IgnoreCase : Boolean read FIgnoreCase;
  252. property Multiline : Boolean Read FMultiLine;
  253. Property Source : string Read FSource;
  254. Property Unicode : boolean Read FUnicode;
  255. {$IFDEF FIREFOX}
  256. // not on all browsers:
  257. property Flags : string read FFlags;
  258. property Sticky : boolean read FSticky;
  259. {$endif}
  260. end;
  261. TReplaceCallBack = reference to Function (Const match : string) : string; varargs;
  262. TReplaceCallBack0 = reference to Function (Const match : string; offset : Integer; AString : String) : string;
  263. TReplaceCallBack1 = reference to Function (Const match,p1 : string; offset : Integer; AString : String) : string;
  264. TReplaceCallBack2 = reference to Function (Const match,p1,p2 : string; offset : Integer; AString : String) : string;
  265. TJSString = class external name 'String'
  266. private
  267. flength : NativeInt; external name 'length';
  268. public
  269. constructor New(Const S : String);
  270. constructor New(Const I : NativeInt);
  271. constructor New(Const D : double);
  272. property length : NativeInt read flength;
  273. class function fromCharCode() : string; varargs;
  274. class function fromCodePoint() : string; varargs;
  275. function anchor(const aName : string) : string;
  276. function charAt(aIndex : NativeInt) : string;
  277. function charCodeAt(aIndex : NativeInt) : NativeInt;
  278. function codePointAt(aIndex : NativeInt) : NativeInt;
  279. function concat(s : string) : string; varargs;
  280. function endsWith(aSearchString : string) : boolean; overload;
  281. function endsWith(aSearchString : string; Pos : NativeInt) : boolean; overload;
  282. function includes(aSearchString : string; Pos : NativeInt = 0) : boolean;
  283. function indexOf(aSearchString : String; Pos : NativeInt = 0) : Integer;
  284. function lastIndexOf(aSearchString : String) : NativeInt;overload;
  285. function lastIndexOf(aSearchString : String; Pos : NativeInt) : Integer;overload;
  286. function link(aUrl : string) : String;
  287. function localeCompare(aCompareString : string) : NativeInt; overload;
  288. function localeCompare(aCompareString : string; aLocales: string) : integer; overload;
  289. function localeCompare(compareString : string; locales: string; Options : TlocaleCompareOptions) : integer; overload;
  290. function match(aRegexp : TJSRegexp) : TStringDynArray; overload;
  291. function match(aRegexp : String) : TStringDynArray;overload;
  292. {$IFDEF ECMAScript6}
  293. function normalize : string;
  294. function normalize(aForm : string) : string;
  295. {$ENDIF}
  296. function _repeat(aCount : NativeInt) : Integer; external name 'repeat';
  297. function replace(aRegexp : String; NewString : String) : String; overload;
  298. function replace(aRegexp : TJSRegexp; NewString : String) : String; overload;
  299. function replace(Regexp : String; aCallback : TReplaceCallBack) : String; overload;
  300. function replace(Regexp : TJSRegexp; aCallback : TReplaceCallBack) : String; overload;
  301. function replace(Regexp : String; aCallback : TReplaceCallBack0) : String; overload;
  302. function replace(Regexp : TJSRegexp; aCallback : TReplaceCallBack0) : String; overload;
  303. function replace(Regexp : String; aCallback : TReplaceCallBack1) : String; overload;
  304. function replace(Regexp : TJSRegexp; aCallback : TReplaceCallBack1) : String; overload;
  305. function replace(Regexp : String; aCallback : TReplaceCallBack2) : String; overload;
  306. function replace(Regexp : TJSRegexp; aCallback : TReplaceCallBack2) : String; overload;
  307. function replaceAll(aRegexp : String; NewString : String) : String; overload;
  308. function replaceAll(aRegexp : TJSRegexp; NewString : String) : String; overload;
  309. function replaceAll(Regexp : String; aCallback : TReplaceCallBack) : String; overload;
  310. function replaceAll(Regexp : TJSRegexp; aCallback : TReplaceCallBack) : String; overload;
  311. function replaceAll(Regexp : String; aCallback : TReplaceCallBack0) : String; overload;
  312. function replaceAll(Regexp : TJSRegexp; aCallback : TReplaceCallBack0) : String; overload;
  313. function replaceAll(Regexp : String; aCallback : TReplaceCallBack1) : String; overload;
  314. function replaceAll(Regexp : TJSRegexp; aCallback : TReplaceCallBack1) : String; overload;
  315. function replaceAll(Regexp : String; aCallback : TReplaceCallBack2) : String; overload;
  316. function replaceAll(Regexp : TJSRegexp; aCallback : TReplaceCallBack2) : String; overload;
  317. function search(Regexp : TJSRegexp) : NativeInt; overload;
  318. function search(Regexp : JSValue) : NativeInt; overload;
  319. function slice(aBeginIndex : NativeInt) : String; overload;
  320. function slice(aBeginIndex, aEndIndex : NativeInt) : String; overload;
  321. function split : TStringDynArray; overload;
  322. function split(aRegexp : TJSRegexp) : TStringDynArray; overload;
  323. function split(aSeparator : string) : TStringDynArray; overload;
  324. function split(aSeparator : string; aLimit : NativeInt) : TStringDynArray; overload;
  325. function split(aSeparator : array of string) : TStringDynArray; overload;
  326. function split(aSeparator : array of string; aLimit : NativeInt) : TStringDynArray; overload;
  327. function startsWith(aSearchString : String) : Boolean; overload;
  328. function startsWith(aSearchString : String; aPosition : NativeInt) : Boolean; overload;
  329. function substr(aStartIndex : NativeInt) : String; overload;
  330. function substr(aStartIndex,aLength : NativeInt) : String; overload;
  331. function substring(aStartIndex : NativeInt) : String; overload;
  332. function substring(aStartIndex,aEndIndex : NativeInt) : String; overload;
  333. function toLocaleLowerCase : String;
  334. function toLocaleUpperCase : String;
  335. function toLowerCase : String;
  336. function toString : string;
  337. function toUpperCase : String;
  338. function trim : string;
  339. function valueOf : string;
  340. end;
  341. TJSArrayEventProc = reference to procedure(element : JSValue; index: NativeInt; anArray : TJSArray);
  342. TJSArrayEvent = reference to function (element : JSValue; index: NativeInt; anArray : TJSArray) : Boolean;
  343. TJSArrayMapEvent = reference to function (element : JSValue; index: NativeInt; anArray : TJSArray) : JSValue;
  344. TJSArrayReduceEvent = reference to function (accumulator, currentValue : JSValue; currentIndex : NativeInt; anArray : TJSArray) : JSValue;
  345. TJSArrayCompareEvent = reference to function (a,b : JSValue) : NativeInt;
  346. TJSArrayCallback = TJSArrayEvent;
  347. TJSArrayMapCallback = TJSArrayMapEvent;
  348. TJSArrayReduceCallBack = TJSArrayReduceEvent;
  349. TJSArrayCompareCallBack = TJSArrayCompareEvent;
  350. { TJSArray }
  351. TJSArray = Class external name 'Array'
  352. private
  353. function GetElements(Index: NativeInt): JSValue; external name '[]';
  354. procedure SetElements(Index: NativeInt; const AValue: JSValue); external name '[]';
  355. public
  356. FLength : NativeInt; external name 'length';
  357. constructor new; overload;
  358. constructor new(aLength : NativeInt); overload;
  359. constructor new(aElement1 : JSValue); varargs; overload;
  360. class function _of() : TJSArray; varargs; external name 'of';
  361. class function isArray(a: JSValue) : Boolean;
  362. {$IFDEF JAVASCRIPT2015}
  363. class function from(a : JSValue) : TJSArray;
  364. class function from(arrayLike : JSValue; mapFunction : TJSMapFunctionCallBack): TJSArray; overload;
  365. class function from(arrayLike : JSValue; mapFunction : TJSMapFunctionCallBack; thisArg : JSValue): TJSArray; overload;
  366. {$ENDIF}
  367. function concat(el : JSValue) : TJSArray; varargs;
  368. function copyWithin(aTarget : NativeInt) : TJSArray;overload; // not in IE
  369. function copyWithin(aTarget, aStart : NativeInt) : TJSArray;overload; // not in IE
  370. function copyWithin(aTarget, aStart, aEnd : NativeInt) : TJSArray;overload; // not in IE
  371. function entries: TJSIterator;
  372. Function every(const aCallback : TJSArrayCallBack) : boolean;overload;
  373. Function every(const aCallback : TJSArrayEvent; aThis : TObject) : boolean;overload;
  374. Function filter(const aCallBack : TJSArrayCallBack) : TJSArray; overload;
  375. Function filter(const aCallBack : TJSArrayEvent; aThis : TObject) : TJSArray;overload;
  376. Function fill(aValue : JSValue) : TJSArray; overload;
  377. Function fill(aValue : JSValue; aStartIndex : NativeInt) : TJSArray; overload;
  378. Function fill(aValue : JSValue; aStartIndex,aEndIndex : NativeInt) : TJSArray; overload;
  379. Function find(const aCallBack : TJSArrayCallBack) : JSValue; overload;
  380. Function find(const aCallBack : TJSArrayEvent; aThis : TObject) : JSValue; overload;
  381. Function findIndex(const aCallBack : TJSArrayCallBack) : NativeInt; overload;
  382. Function findIndex(const aCallBack : TJSArrayEvent; aThis : TObject) : NativeInt; overload;
  383. procedure forEach(const aCallBack : TJSArrayEventProc); overload;
  384. procedure forEach(const aCallBack : TJSArrayEvent); overload;
  385. procedure forEach(const aCallBack : TJSArrayEvent; aThis : TObject); overload;
  386. function includes(aElement : JSValue) : Boolean; overload;
  387. function includes(aElement : JSValue; FromIndex : NativeInt) : Boolean; overload;
  388. function indexOf(aElement : JSValue) : NativeInt; overload;
  389. function indexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  390. function join : String; overload;
  391. function join (aSeparator : string) : String; overload;
  392. function keys: TJSIterator;
  393. function lastIndexOf(aElement : JSValue) : NativeInt; overload;
  394. function lastIndexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  395. // Function map(const aCallBack : TJSArrayMapEventArray) : JSValue; overload;
  396. Function map(const aCallBack : TJSArrayMapCallBack) : TJSArray; overload;
  397. Function map(const aCallBack : TJSArrayMapEvent; aThis : TObject) : TJSArray; overload;
  398. function pop : JSValue;
  399. function push(aElement : JSValue) : NativeInt; varargs;
  400. function reduce(const aCallBack : TJSArrayReduceCallBack) : JSValue; overload;
  401. function reduce(const aCallBack : TJSArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  402. function reduceRight(const aCallBack : TJSArrayReduceCallBack) : JSValue; overload;
  403. function reduceRight(const aCallBack : TJSArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  404. Function reverse : TJSArray;
  405. Function shift : JSValue;
  406. Function slice : TJSArray; overload;
  407. function slice(aBegin : NativeInt) : TJSArray; overload;
  408. function slice(aBegin,aEnd : NativeInt) : TJSArray; overload;
  409. Function some(const aCallback : TJSArrayCallBack) : boolean; overload;
  410. Function some(const aCallback : TJSArrayEvent; aThis : TObject) : boolean; overload;
  411. Function sort(const aCallback : TJSArrayCompareCallBack) : TJSArray; overload;
  412. Function sort() : TJSArray; overload;
  413. function splice(aStart : NativeInt) : TJSArray; overload;
  414. function splice(aStart,aDeleteCount : NativeInt) : TJSArray; varargs; overload;
  415. function toLocaleString: String; overload;
  416. function toLocaleString(locales : string) : String; overload;
  417. function toLocaleString(locales : string; const Options : TLocaleCompareOptions) : String; overload;
  418. function toString : String;
  419. function unshift : NativeInt; varargs;
  420. function values: TJSIterator;
  421. Property Length : NativeInt Read FLength Write FLength;
  422. property Elements[Index: NativeInt]: JSValue read GetElements write SetElements; default;
  423. end;
  424. TJSArrayBuffer = Class external name 'ArrayBuffer' (TJSObject)
  425. private
  426. fLength : NativeInt; external name 'byteLength';
  427. public
  428. constructor new(aByteLength : NativeInt);
  429. class function isView(aValue : JSValue) : Boolean;
  430. function slice(aBegin : NativeInt) : TJSArrayBuffer; overload;
  431. function slice(aBegin,aEnd : NativeInt) : TJSArrayBuffer; overload;
  432. Property byteLength : NativeInt Read fLength;
  433. end;
  434. TJSBufferSource = class external name 'BufferSource'
  435. end;
  436. { TJSTypedArray }
  437. TJSTypedArray = Class;
  438. TJSTypedArrayCallBack = reference to function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : Boolean;
  439. TJSTypedArrayEvent = TJSTypedArrayCallBack; // function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : Boolean of object;
  440. TJSTypedArrayMapCallBack = reference to function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : JSValue;
  441. TJSTypedArrayMapEvent = TJSTypedArrayMapCallBack; // reference to function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : JSValue of object;
  442. TJSTypedArrayReduceCallBack = reference to function (accumulator, currentValue : JSValue; currentIndex : NativeInt; anArray : TJSTypedArray) : JSValue;
  443. TJSTypedArrayCompareCallBack = reference to function (a,b : JSValue) : NativeInt;
  444. TJSTypedArray = class external name 'TypedArray' (TJSBufferSource)
  445. Private
  446. FBuffer: TJSArrayBuffer; external name 'buffer';
  447. FByteLength: NativeInt; external name 'byteLength';
  448. FLength: NativeInt; external name 'length';
  449. FByteOffset: NativeInt; external name 'byteOffset';
  450. FBytesPerElement : NativeInt; external name 'BYTES_PER_ELEMENT';
  451. function getValue(Index : NativeInt) : JSValue; external name '[]';
  452. procedure setValue(Index : NativeInt;AValue : JSValue); external name '[]';
  453. Public
  454. property BYTES_PER_ELEMENT : NativeInt Read FBytesPerElement;
  455. class var name : string;
  456. // class function from(aValue : jsValue) : TJSTypedArray;
  457. // class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSTypedArray;
  458. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSTypedArray;
  459. class function _of(aValue : jsValue) : TJSTypedArray; varargs; external name 'of';
  460. function copyWithin(aTarget : NativeInt) : TJSTypedArray;overload;
  461. function copyWithin(aTarget, aStart : NativeInt) : TJSTypedArray;overload;
  462. function copyWithin(aTarget, aStart, aEnd : NativeInt) : TJSTypedArray;overload;
  463. Function every(const aCallback : TJSTypedArrayCallBack) : boolean;overload;
  464. Function every(const aCallback : TJSTypedArrayEvent; aThis : TObject) : boolean;overload;
  465. Function fill(aValue : JSValue) : TJSTypedArray; overload;
  466. Function fill(aValue : JSValue; aStartIndex : NativeInt) : TJSTypedArray; overload;
  467. Function fill(aValue : JSValue; aStartIndex,aEndIndex : NativeInt) : TJSTypedArray; overload;
  468. Function filter(const aCallBack : TJSTypedArrayCallBack) : TJSTypedArray; overload;
  469. Function filter(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : TJSTypedArray;overload;
  470. Function find(const aCallBack : TJSTypedArrayCallBack) : JSValue; overload;
  471. Function find(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : JSValue; overload;
  472. Function findIndex(const aCallBack : TJSTypedArrayCallBack) : NativeInt; overload;
  473. Function findIndex(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : NativeInt; overload;
  474. procedure forEach(const aCallBack : TJSTypedArrayCallBack); overload;
  475. procedure forEach(const aCallBack : TJSTypedArrayEvent; aThis : TObject); overload;
  476. function includes(aElement : JSValue) : Boolean; overload;
  477. function includes(aElement : JSValue; FromIndex : NativeInt) : Boolean; overload;
  478. function indexOf(aElement : JSValue) : NativeInt; overload;
  479. function indexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  480. function join : String; overload;
  481. function join (aSeparator : string) : String; overload;
  482. function lastIndexOf(aElement : JSValue) : NativeInt; overload;
  483. function lastIndexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  484. Function map(const aCallBack : TJSTypedArrayCallBack) : TJSTypedArray; overload;
  485. Function map(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : TJSTypedArray; overload;
  486. function reduce(const aCallBack : TJSTypedArrayReduceCallBack) : JSValue; overload;
  487. function reduce(const aCallBack : TJSTypedArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  488. function reduceRight(const aCallBack : TJSTypedArrayReduceCallBack) : JSValue; overload;
  489. function reduceRight(const aCallBack : TJSTypedArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  490. Function reverse : TJSTypedArray;
  491. procedure _set(anArray : TJSArray); external name 'set';
  492. procedure _set(anArray : TJSArray; anOffset : NativeInt); external name 'set';
  493. procedure _set(anArray : TJSTypedArray); external name 'set';
  494. procedure _set(anArray : TJSTypedArray; anOffset : NativeInt); external name 'set';
  495. Function slice : TJSTypedArray; overload;
  496. function slice(aBegin : NativeInt) : TJSTypedArray; overload;
  497. function slice(aBegin,aEnd : NativeInt) : TJSTypedArray; overload;
  498. Function some(const aCallback : TJSTypedArrayCallBack) : boolean; overload;
  499. Function some(const aCallback : TJSTypedArrayEvent; aThis : TObject) : boolean; overload;
  500. Function sort(const aCallback : TJSTypedArrayCompareCallBack) : TJSTypedArray; overload;
  501. Function sort() : TJSTypedArray; overload;
  502. function splice(aStart : NativeInt) : TJSTypedArray; overload;
  503. function splice(aStart,aDeleteCount : NativeInt) : TJSTypedArray; varargs; overload;
  504. function toLocaleString: String; overload;
  505. function toLocaleString(locales : string) : String; overload;
  506. function toLocaleString(locales : string; const Options : TLocaleCompareOptions) : String; overload;
  507. function toString : String;
  508. function unshift : NativeInt; varargs;
  509. property buffer : TJSArrayBuffer read FBuffer;
  510. property byteLength : NativeInt Read FByteLength;
  511. property byteOffset : NativeInt Read FByteOffset;
  512. property length : NativeInt Read FLength;
  513. property values[Index : NativeInt] : JSValue Read getValue Write SetValue; default;
  514. end;
  515. { TJSInt8Array }
  516. TJSInt8Array = class external name 'Int8Array' (TJSTypedArray)
  517. private
  518. function getTypedValue(Index : NativeInt): Shortint; external name '[]';
  519. procedure setTypedValue(Index : NativeInt; AValue: Shortint);external name '[]';
  520. public
  521. {$IFDEF JAVASCRIPT2017}
  522. constructor new; // new in ES2017
  523. {$ENDIF}
  524. constructor new (length : NativeInt);
  525. constructor new (atypedArray : TJSTypedArray);
  526. constructor new (aObject : TJSObject);
  527. constructor new (buffer : TJSArrayBuffer);
  528. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  529. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  530. class function from(aValue : jsValue) : TJSInt8Array; reintroduce;
  531. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSInt8Array; reintroduce;
  532. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSInt8Array; reintroduce;
  533. class function _of(aValue : jsValue) : TJSInt8Array; varargs; external name 'of'; reintroduce;overload;
  534. class function _of(aValue : TJSValueDynArray) : TJSInt8Array; varargs; external name 'of'; reintroduce; overload;
  535. function subarray(aBegin, aEnd: Integer): TJSInt8Array; overload;
  536. function subarray(aBegin: Integer): TJSInt8Array; overload;
  537. procedure _set(anArray : Array of ShortInt); external name 'set'; reintroduce; overload;
  538. procedure _set(anArray : Array of ShortInt; anOffset : NativeInt); external name 'set';
  539. property values[Index : NativeInt] : Shortint Read getTypedValue Write setTypedValue; default;
  540. end;
  541. TJSUint8Array = class external name 'Uint8Array' (TJSTypedArray)
  542. private
  543. function getTypedValue(Index : NativeInt): Byte; external name '[]';
  544. procedure setTypedValue(Index : NativeInt; AValue: Byte);external name '[]';
  545. public
  546. constructor new (length : NativeInt);
  547. constructor new (atypedArray : TJSTypedArray);
  548. constructor new (aObject : TJSObject);
  549. constructor new (buffer : TJSArrayBuffer);
  550. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  551. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  552. class function from(aValue : jsValue) : TJSUInt8Array; reintroduce; overload;
  553. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSUInt8Array; reintroduce;overload;
  554. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSUInt8Array; reintroduce;overload;
  555. class function _of(aValue : jsValue) : TJSUInt8Array; varargs; external name 'of'; reintroduce; overload;
  556. function subarray(aBegin, aEnd: Integer): TJSUInt8Array; overload;
  557. function subarray(aBegin: Integer): TJSUInt8Array; overload;
  558. procedure _set(anArray : Array of Byte); external name 'set'; reintroduce; overload;
  559. procedure _set(anArray : Array of Byte; anOffset : NativeInt); external name 'set'; overload;
  560. Property values[Index : NativeInt] : Byte Read getTypedValue Write setTypedValue; default;
  561. end;
  562. TJSUint8ClampedArray = class external name 'Uint8ClampedArray' (TJSTypedArray)
  563. private
  564. function getTypedValue(Index : NativeInt): Byte; external name '[]';
  565. procedure setTypedValue(Index : NativeInt; AValue: Byte);external name '[]';
  566. public
  567. constructor new (length : NativeInt);
  568. constructor new (atypedArray : TJSTypedArray);
  569. constructor new (aObject : TJSObject);
  570. constructor new (buffer : TJSArrayBuffer);
  571. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  572. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  573. class function from(aValue : jsValue) : TJSUInt8ClampedArray; reintroduce;
  574. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSUInt8ClampedArray; reintroduce;overload;
  575. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSUInt8ClampedArray; reintroduce;overload;
  576. class function _of(aValue : jsValue) : TJSUInt8ClampedArray; varargs; external name 'of'; reintroduce;
  577. procedure _set(anArray : Array of Byte); external name 'set'; reintroduce;overload;
  578. procedure _set(anArray : Array of Byte; anOffset : NativeInt); external name 'set';overload;
  579. function subarray(aBegin, aEnd: Integer): TJSUInt8ClampedArray; overload;
  580. function subarray(aBegin: Integer): TJSUInt8ClampedArray; overload;
  581. Property values[Index : NativeInt] : Byte Read getTypedValue Write setTypedValue; default;
  582. end;
  583. TJSInt16Array = class external name 'Int16Array' (TJSTypedArray)
  584. private
  585. function getTypedValue(Index : NativeInt): smallint; external name '[]';
  586. procedure setTypedValue(Index : NativeInt; AValue: Smallint);external name '[]';
  587. public
  588. constructor new (length : NativeInt);
  589. constructor new (atypedArray : TJSTypedArray);
  590. constructor new (aObject : TJSObject);
  591. constructor new (buffer : TJSArrayBuffer);
  592. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  593. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  594. class function from(aValue : jsValue) : TJSInt16Array; reintroduce;
  595. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSInt16Array; reintroduce;overload;
  596. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSInt16Array; reintroduce;overload;
  597. class function _of(aValue : jsValue) : TJSInt16Array; varargs; external name 'of'; reintroduce;overload;
  598. procedure _set(anArray : Array of SmallInt); external name 'set'; reintroduce;overload;
  599. procedure _set(anArray : Array of SmallInt; anOffset : NativeInt); external name 'set';overload;
  600. function subarray(aBegin, aEnd: Integer): TJSInt16Array; overload;
  601. function subarray(aBegin: Integer): TJSInt16Array; overload;
  602. Property values[Index : NativeInt] : SmallInt Read getTypedValue Write setTypedValue; default;
  603. end;
  604. TJSUint16Array = class external name 'Uint16Array' (TJSTypedArray)
  605. private
  606. function getTypedValue(Index : NativeInt): Word; external name '[]';
  607. procedure setTypedValue(Index : NativeInt; AValue: Word);external name '[]';
  608. public
  609. constructor new (length : NativeInt);
  610. constructor new (atypedArray : TJSTypedArray);
  611. constructor new (aObject : TJSObject);
  612. constructor new (buffer : TJSArrayBuffer);
  613. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  614. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  615. class function from(aValue : jsValue) : TJSUInt16Array; reintroduce;
  616. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSUInt16Array; reintroduce;
  617. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSUInt16Array; reintroduce;
  618. class function _of(aValue : jsValue) : TJSUInt16Array; varargs; external name 'of'; reintroduce;
  619. procedure _set(anArray : Array of Word); external name 'set'; reintroduce; overload;
  620. procedure _set(anArray : Array of Word; anOffset : NativeInt); external name 'set'; overload;
  621. function subarray(aBegin, aEnd: Integer): TJSUInt16Array; overload;
  622. function subarray(aBegin: Integer): TJSUInt16Array; overload;
  623. Property values[Index : NativeInt] : Word Read getTypedValue Write setTypedValue; default;
  624. end;
  625. TJSInt32Array = class external name 'Int32Array' (TJSTypedArray)
  626. private
  627. function getTypedValue(Index : NativeInt): longint; external name '[]';
  628. procedure setTypedValue(Index : NativeInt; AValue: longint);external name '[]';
  629. public
  630. constructor new (length : NativeInt);
  631. constructor new (atypedArray : TJSTypedArray);
  632. constructor new (aObject : TJSObject);
  633. constructor new (buffer : TJSArrayBuffer);
  634. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  635. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  636. class function from(aValue : jsValue) : TJSInt32Array; reintroduce;
  637. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSInt32Array; reintroduce;
  638. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSInt32Array; reintroduce;
  639. class function _of(aValue : jsValue) : TJSInt32Array; varargs;external name 'of'; reintroduce;
  640. procedure _set(anArray : Array of LongInt); external name 'set'; reintroduce; overload;
  641. procedure _set(anArray : Array of LongInt; anOffset : NativeInt); external name 'set'; overload;
  642. function subarray(aBegin, aEnd: Integer): TJSInt32Array; overload;
  643. function subarray(aBegin: Integer): TJSInt32Array; overload;
  644. Property values[Index : NativeInt] : longint Read getTypedValue Write setTypedValue; default;
  645. end;
  646. TJSUint32Array = class external name 'Uint32Array' (TJSTypedArray)
  647. private
  648. function getTypedValue(Index : NativeInt): LongWord; external name '[]';
  649. procedure setTypedValue(Index : NativeInt; AValue: LongWord);external name '[]';
  650. public
  651. constructor new (length : NativeInt);
  652. constructor new (atypedArray : TJSTypedArray);
  653. constructor new (aObject : TJSObject);
  654. constructor new (buffer : TJSArrayBuffer);
  655. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  656. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  657. class function from(aValue : jsValue) : TJSUInt32Array; reintroduce;
  658. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSUInt32Array; reintroduce;
  659. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSUInt32Array; reintroduce;
  660. class function _of(aValue : jsValue) : TJSUInt32Array; varargs; external name 'of'; reintroduce;
  661. procedure _set(anArray : Array of Cardinal); external name 'set'; reintroduce; overload;
  662. procedure _set(anArray : Array of Cardinal; anOffset : NativeInt); external name 'set'; overload;
  663. function subarray(aBegin, aEnd: Integer): TJSUInt32Array; overload;
  664. function subarray(aBegin: Integer): TJSUInt32Array; overload;
  665. Property values[Index : NativeInt] : LongWord Read getTypedValue Write setTypedValue; default;
  666. end;
  667. TJSFloat32Array = class external name 'Float32Array' (TJSTypedArray)
  668. private
  669. function getTypedValue(Index : NativeInt): Float32; external name '[]';
  670. procedure setTypedValue(Index : NativeInt; AValue: Float32);external name '[]';
  671. public
  672. constructor new (length : NativeInt);
  673. constructor new (atypedArray : TJSTypedArray);
  674. constructor new (aObject : TJSObject);
  675. constructor new (buffer : TJSArrayBuffer);
  676. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  677. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  678. class function from(aValue : jsValue) : TJSFloat32Array; reintroduce;
  679. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSFloat32Array; reintroduce;
  680. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSFloat32Array; reintroduce;
  681. class function _of(aValue : jsValue) : TJSFloat32Array; varargs; reintroduce;
  682. procedure _set(anArray : Array of Double); external name 'set'; reintroduce; overload;
  683. procedure _set(anArray : Array of Double; anOffset : NativeInt); external name 'set'; reintroduce; overload;
  684. function subarray(aBegin, aEnd: Integer): TJSFloat32Array; overload;
  685. function subarray(aBegin: Integer): TJSFloat32Array; overload;
  686. Property values[Index : NativeInt] : Float32 Read getTypedValue Write setTypedValue; default;
  687. end;
  688. TJSFloat64Array = class external name 'Float64Array' (TJSTypedArray)
  689. private
  690. function getTypedValue(Index : NativeInt): Float64; external name '[]';
  691. procedure setTypedValue(Index : NativeInt; AValue: Float64);external name '[]';
  692. public
  693. constructor new (length : NativeInt);
  694. constructor new (atypedArray : TJSTypedArray);
  695. constructor new (aObject : TJSObject);
  696. constructor new (buffer : TJSArrayBuffer);
  697. constructor new (buffer : TJSArrayBuffer; aByteOffset: NativeInt);
  698. constructor new (buffer : TJSArrayBuffer; aByteOffset, aLength: NativeInt);
  699. class function from(aValue : jsValue) : TJSFloat64Array; reintroduce;
  700. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSFloat64Array; reintroduce;
  701. // class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSFloat64Array; reintroduce;
  702. class function _of(aValue : jsValue) : TJSFloat64Array; varargs; reintroduce;
  703. procedure _set(anArray : Array of Double); external name 'set'; reintroduce; overload;
  704. procedure _set(anArray : Array of Double; anOffset : NativeInt); external name 'set'; reintroduce; overload;
  705. function subarray(aBegin, aEnd: Integer): TJSFloat64Array; overload;
  706. function subarray(aBegin: Integer): TJSFloat64Array; overload;
  707. Property values[Index : NativeInt] : Float64 Read getTypedValue Write setTypedValue; default;
  708. end;
  709. TJSDataView = Class external name 'DataView' (TJSBufferSource)
  710. private
  711. fBuffer : TJSArrayBuffer; external name 'buffer';
  712. fLength : NativeInt; external name 'byteLength';
  713. fOffset : NativeInt; external name 'byteOffset';
  714. public
  715. constructor new(aBuffer : TJSArrayBuffer); overload;
  716. constructor new(aBuffer : TJSArrayBuffer; aOffset : NativeInt); overload;
  717. constructor new(aBuffer : TJSArrayBuffer; aOffset,aByteLength : NativeInt); overload;
  718. function getFloat32(aByteOffset : NativeInt) : double; overload;
  719. function getFloat32(aByteOffset : NativeInt; aLittleEndian: Boolean) : double; overload;
  720. function getFloat64(aByteOffset : NativeInt) : double; overload;
  721. function getFloat64(aByteOffset : NativeInt; aLittleEndian: Boolean) : double; overload;
  722. function getInt8(aByteOffset : NativeInt) : ShortInt;
  723. function getInt16(aByteOffset : NativeInt) : SmallInt; overload;
  724. function getInt16(aByteOffset : NativeInt; aLittleEndian : Boolean) : SmallInt; overload;
  725. function getInt32(aByteOffset : NativeInt) : Longint; overload;
  726. function getInt32(aByteOffset : NativeInt; aLittleEndian : Boolean) : Longint; overload;
  727. function getUint8(aByteOffset : NativeInt) : Byte; overload;
  728. function getUint16(aByteOffset : NativeInt) : Word; overload;
  729. function getUint16(aByteOffset : NativeInt; aLittleEndian : Boolean) : Word; overload;
  730. function getUint32(aByteOffset : NativeInt) : LongWord; overload;
  731. function getUint32(aByteOffset : NativeInt; aLittleEndian : Boolean) : LongWord; overload;
  732. procedure setFloat32(aByteOffset : NativeInt; aValue : double); overload;
  733. procedure setFloat32(aByteOffset : NativeInt; aValue : double; aLittleEndian: Boolean); overload;
  734. procedure setFloat64(aByteOffset : NativeInt; aValue : double); overload;
  735. procedure setFloat64(aByteOffset : NativeInt; aValue : double; aLittleEndian: Boolean); overload;
  736. procedure setInt8(aByteOffset : NativeInt; aValue : ShortInt);
  737. procedure setInt16(aByteOffset : NativeInt; aValue : SmallInt); overload;
  738. procedure setInt16(aByteOffset : NativeInt; aValue : SmallInt; aLittleEndian : Boolean); overload;
  739. procedure setInt32(aByteOffset : NativeInt; aValue : Longint); overload;
  740. procedure setInt32(aByteOffset : NativeInt; aValue : Longint; aLittleEndian : Boolean); overload;
  741. procedure setUint8(aByteOffset : NativeInt; aValue : Byte); overload;
  742. procedure setUint16(aByteOffset : NativeInt; aValue : Word); overload;
  743. procedure setUint16(aByteOffset : NativeInt; aValue : Word; aLittleEndian : Boolean); overload;
  744. procedure setUint32(aByteOffset : NativeInt; aValue : LongWord); overload;
  745. procedure setUint32(aByteOffset : NativeInt; aValue: LongWord; aLittleEndian : Boolean); overload;
  746. Property byteLength : NativeInt Read fLength;
  747. Property byteOffset : NativeInt read fOffset;
  748. property buffer : TJSArrayBuffer Read fBuffer;
  749. end;
  750. TJSJSON = class external name 'JSON' (TJSObject)
  751. Public
  752. class function parse(aJSON : String) : JSValue;
  753. // Use this only when you are sure you will get an object, no checking is done.
  754. class function parseObject(aJSON : String) : TJSObject; external name 'parse';
  755. class function stringify(aValue : JSValue) : string;
  756. class function stringify(aValue,aReplacer : JSValue) : string;
  757. class function stringify(aValue,aReplacer : JSValue; space: NativeInt) : string;
  758. class function stringify(aValue,aReplacer : JSValue; space: String) : string;
  759. end;
  760. { TJSError }
  761. TJSError = Class external name 'Error' (TJSObject)
  762. private
  763. FMessage: String; external name 'message';
  764. {$ifdef NodeJS}
  765. FStack: JSValue; external name 'stack';
  766. {$endif}
  767. Public
  768. Constructor new;
  769. Constructor new(Const aMessage : string);
  770. Constructor new(Const aMessage,aFileName : string);
  771. Constructor new(Const aMessage,aFileName : string; aLineNumber : NativeInt);
  772. Property Message : String Read FMessage;
  773. {$ifdef NodeJS}
  774. Property Stack: JSValue read FStack;
  775. {$endif}
  776. end;
  777. TJSPromiseResolver = reference to function (aValue : JSValue) : JSValue;
  778. TJSPromiseExecutor = reference to procedure (resolve,reject : TJSPromiseResolver);
  779. TJSPromiseFinallyHandler = reference to procedure;
  780. TJSPromise = Class;
  781. TJSPromiseArray = array of TJSPromise;
  782. TJSPromise = class external name 'Promise'
  783. constructor new(Executor : TJSPromiseExecutor);
  784. class function all(arg : Array of JSValue) : TJSPromise; overload;
  785. class function all(arg : JSValue) : TJSPromise; overload;
  786. class function all(arg : TJSPromiseArray) : TJSPromise; overload;
  787. class function allSettled(arg : Array of JSValue) : TJSPromise; overload;
  788. class function allSettled(arg : JSValue) : TJSPromise; overload;
  789. class function allSettled(arg : TJSPromiseArray) : TJSPromise; overload;
  790. class function race(arg : Array of JSValue) : TJSPromise; overload;
  791. class function race(arg : JSValue) : TJSPromise; overload;
  792. class function race(arg : TJSPromiseArray) : TJSPromise; overload;
  793. class function reject(reason : JSValue) : TJSPromise;
  794. class function resolve(value : JSValue): TJSPromise; overload;
  795. class function resolve : TJSPromise; overload;
  796. function _then (onAccepted : TJSPromiseResolver) : TJSPromise; external name 'then';
  797. function _then (onAccepted,OnRejected: TJSPromiseResolver) : TJSPromise; external name 'then';
  798. function catch (onRejected : TJSPromiseResolver) : TJSPromise;
  799. function _finally(value : TJSPromiseFinallyHandler): TJSPromise; external name 'finally';
  800. end;
  801. generic TGPromise<T> = class external name 'Promise'
  802. Type
  803. TResolve = reference to function (aValue : T) : JSValue;
  804. TReject = reference to function (aValue : JSValue) : JSValue;
  805. TExecute = reference to procedure (resolve: TResolve; reject : TReject);
  806. TFinallyHandler = reference to procedure;
  807. Public
  808. constructor new(Executor : TExecute);
  809. function then_ (onAccepted : TResolve) : TJSPromise; external name 'then';
  810. function then_ (onAccepted : TResolve; onRejected : TReject) : TJSPromise; external name 'then';
  811. function finally_(value : TFinallyHandler): TJSPromise; external name 'finally';
  812. function catch (onRejected : TReject) : TJSPromise;
  813. end;
  814. generic TGPromiseEx<T,E> = class external name 'Promise'
  815. Type
  816. TResolve = reference to function (aValue : T) : JSValue;
  817. TReject = reference to function (aValue : E) : JSValue;
  818. TExecute = reference to procedure (resolve: TResolve; reject : TReject);
  819. TFinallyHandler = reference to procedure;
  820. Public
  821. constructor new(Executor : TExecute);
  822. function then_ (onAccepted : TResolve) : TJSPromise; external name 'then';
  823. function then_ (onAccepted : TResolve; onRejected : TReject) : TJSPromise; external name 'then';
  824. function catch (onRejected : TReject) : TJSPromise;
  825. function finally_(value : TFinallyHandler): TJSPromise; external name 'finally';
  826. end;
  827. TJSFunctionArguments = class external name 'arguments'
  828. private
  829. FLength: NativeInt; external name 'length';
  830. function GetElements(Index: NativeInt): JSValue; external name '[]';
  831. procedure SetElements(Index: NativeInt; const AValue: JSValue); external name '[]';
  832. public
  833. property Length: NativeInt read FLength;
  834. property Elements[Index: NativeInt]: JSValue read GetElements write SetElements; default;
  835. end;
  836. TJSIteratorResult = Class external name 'IteratorResult' (TJSObject)
  837. Private
  838. fDone : Boolean; external name 'done';
  839. fValue : JSValue; external name 'value';
  840. Public
  841. property done : boolean Read FDone;
  842. property value : JSValue read FValue;
  843. end;
  844. TJSAsyncIterator = Class external name 'AsyncIterator' (TJSObject)
  845. function next: TJSIteratorResult;
  846. end;
  847. TJSSyntaxError = class external name 'SyntaxError' (TJSError);
  848. TJSTextDecoderOptions = class external name 'Object' (TJSObject)
  849. fatal : Boolean;
  850. ignoreBOM : Boolean;
  851. end;
  852. TJSTextDecodeOptions = class external name 'Object' (TJSObject)
  853. stream : Boolean;
  854. end;
  855. TJSTextDecoder = class external name 'TextDecoder' (TJSObject)
  856. Private
  857. FEncoding : String; external name 'encoding';
  858. FFatal : Boolean; external name 'fatal';
  859. FIgnoreBOM : Boolean; external name 'ignoreBOM';
  860. Public
  861. Constructor New(utfLabel : String); overload;
  862. Constructor New(utfLabel : String; Options : TJSTextDecoderOptions); overload;
  863. Function decode(arr : TJSTypedArray) : String; overload;
  864. Function decode(arr : TJSArrayBuffer) : String; overload;
  865. Function decode(arr : TJSTypedArray; opts : TJSTextDecodeOptions) : String; overload;
  866. Function decode(arr : TJSArrayBuffer; opts : TJSTextDecodeOptions) : String; overload;
  867. property Encoding : string Read FEncoding;
  868. Property Fatal : Boolean Read FFatal;
  869. Property IgnoreBOM : Boolean Read FIgnoreBOM;
  870. end;
  871. TJSTextEncoderEncodeIntoResult = class external name 'Object' (TJSObject)
  872. read : Nativeint;
  873. written : NativeInt;
  874. end;
  875. TJSTextEncoder = class external name 'TextEncoder' (TJSObject)
  876. Private
  877. FEncoding : String; external name 'encoding';
  878. Public
  879. Constructor New;
  880. function encode(aString : String) : TJSUInt8Array;
  881. Function encodeInto(aString : String; aArray : TJSUInt8Array) : TJSTextEncoderEncodeIntoResult;
  882. Property Encoding : string Read FEncoding;
  883. end;
  884. var
  885. // JSArguments can be used in procedures/functions to provide access to the 'arguments' array.
  886. JSArguments: TJSFunctionArguments; external name 'arguments';
  887. // JSThis can be used in all code to access the javascript 'this' object.
  888. JSThis: TJSObject; external name 'this';
  889. // JSExceptValue can be used in catch blocks to access the JS throw value
  890. JSExceptValue: JSValue; external name '$e';
  891. function new(aElements: TJSValueDynArray) : TJSObject; overload;
  892. function JSDelete(const Obj: JSValue; const PropName: string): boolean; assembler; overload;
  893. function decodeURIComponent(encodedURI : String) : String; external name 'decodeURIComponent';
  894. function encodeURIComponent(str : String) : String; external name 'encodeURIComponent';
  895. function parseInt(s: String; Radix: NativeInt): NativeInt; overload; external name 'parseInt'; // may result NaN
  896. function parseInt(s: String): NativeInt; overload; external name 'parseInt'; // may result NaN
  897. function parseFloat(s: String): double; overload; external name 'parseFloat'; // may result NaN
  898. function hasString(const v: JSValue): boolean; external name 'rtl.hasString';// isString(v) and v<>''
  899. function hasValue(const v: JSValue): boolean; assembler; // returns the JS definition of if(v): v is not false, undefined, null, 0, NaN, or the empty string. Note: JS if(new Boolean(false)) returns true.
  900. function &in(const keyName: String; const &object: TJSObject): Boolean; assembler;
  901. function isArray(const v: JSValue): boolean; external name 'rtl.isArray';
  902. function isBoolean(const v: JSValue): boolean; assembler;
  903. function isDate(const v: JSValue): boolean; assembler;
  904. function isCallback(const v: JSValue): boolean; assembler;
  905. function isChar(const v: JSValue): boolean; assembler;
  906. function isClass(const v: JSValue): boolean; assembler; // is a Pascal class, e.g. a TClass
  907. function isClassInstance(const v: JSValue): boolean; assembler;// is a Pascal class instance, e.g. a TObject
  908. function isFunction(const v: JSValue): boolean; external name 'rtl.isFunction';
  909. function isInteger(const v: JSValue): boolean; assembler;
  910. function isModule(const v: JSValue): boolean; external name 'rtl.isModule';
  911. function isNull(const v: JSValue): boolean; assembler;
  912. function isNumber(const v: JSValue): boolean; external name 'rtl.isNumber';
  913. function isObject(const v: JSValue): boolean; external name 'rtl.isObject'; // true if not null and a JS Object
  914. function isRecord(const v: JSValue): boolean; assembler;
  915. function isString(const v: JSValue): boolean; external name 'rtl.isString';
  916. function isUndefined(const v: JSValue): boolean; assembler;
  917. function isDefined(const v: JSValue): boolean; assembler;
  918. function isUTF16Char(const v: JSValue): boolean; assembler;
  919. function isExt(const InstanceOrClass, aClass: JSValue): boolean; external name 'rtl.isExt'; // aClass can be a JS object or function
  920. function jsInstanceOf(const aFunction, aFunctionWithPrototype: JSValue): Boolean; assembler;
  921. function jsTypeOf(const v: JSValue): String; external name 'typeof';
  922. function jsIsNaN(const v: JSValue): boolean; external name 'isNaN';// true if value cannot be converted to a number. e.g. True on NaN, undefined, {}, '123'. False on true, null, '', ' ', '1A'
  923. function jsIsFinite(const v: JSValue): boolean; external name 'isFinite';// true if value is a Finite number
  924. function toNumber(const v: JSValue): double; assembler; // if not possible, returns NaN
  925. function toInteger(const v: JSValue): NativeInt; // if v is not an integer, returns 0
  926. function toObject(Value: JSValue): TJSObject; // If Value is not a Javascript object, returns Nil
  927. function toArray(Value: JSValue): TJSArray; // If Value is not a Javascript array, returns Nil
  928. function toBoolean(Value: JSValue): Boolean; // If Value is not a Boolean, returns False
  929. function toString(Value: JSValue): String; // If Value is not a string, returns ''
  930. Type
  931. TJSValueType = (jvtNull,jvtBoolean,jvtInteger,jvtFloat,jvtString,jvtObject,jvtArray);
  932. Function GetValueType(JS : JSValue) : TJSValueType;
  933. Const
  934. Null : JSValue; external name 'null';
  935. Undefined : JSValue; external name 'undefined';
  936. implementation
  937. function new(aElements: TJSValueDynArray): TJSObject;
  938. function toString(I : Integer): string; external name 'String';
  939. Var
  940. L,I : integer;
  941. S : String;
  942. begin
  943. L:=length(aElements);
  944. if (L mod 2)=1 then
  945. raise EJS.Create('Number of arguments must be even');
  946. I:=0;
  947. // Check all arguments;
  948. While (i<L) do
  949. begin
  950. if Not isString(aElements[i]) then
  951. begin
  952. S:=ToString(I);
  953. raise EJS.Create('Argument '+S+' must be a string.');
  954. end;
  955. inc(I,2);
  956. end;
  957. I:=0;
  958. Result:=TJSObject.New;
  959. While (i<L) do
  960. begin
  961. S:=String(aElements[i]);
  962. Result.Properties[S]:=aElements[i+1];
  963. inc(I,2);
  964. end;
  965. end;
  966. function JSDelete(const Obj: JSValue; const PropName: string): boolean; assembler;
  967. asm
  968. return delete Obj[PropName];
  969. end;
  970. function hasValue(const v: JSValue): boolean; assembler;
  971. asm
  972. if(v){ return true; } else { return false; };
  973. end;
  974. function &in(const keyName: String; const &object: TJSObject): Boolean; assembler;
  975. asm
  976. return keyName in object;
  977. end;
  978. function isBoolean(const v: JSValue): boolean; assembler;
  979. asm
  980. return typeof(v) == 'boolean';
  981. end;
  982. function isDate(const v: JSValue): boolean; assembler;
  983. asm
  984. return (v instanceof Date);
  985. end;
  986. function isCallback(const v: JSValue): boolean; assembler;
  987. asm
  988. return rtl.isObject(v) && rtl.isObject(v.scope) && (rtl.isString(v.fn) || rtl.isFunction(v.fn));
  989. end;
  990. function isChar(const v: JSValue): boolean; assembler;
  991. asm
  992. return (typeof(v)!="string") && (v.length==1);
  993. end;
  994. function isClass(const v: JSValue): boolean; assembler;
  995. asm
  996. return (typeof(v)=="object") && (v!=null) && (v.$class == v);
  997. end;
  998. function isClassInstance(const v: JSValue): boolean; assembler;
  999. asm
  1000. return (typeof(v)=="object") && (v!=null) && (v.$class == Object.getPrototypeOf(v));
  1001. end;
  1002. function isInteger(const v: JSValue): boolean; assembler;
  1003. asm
  1004. return Math.floor(v)===v;
  1005. end;
  1006. function isNull(const v: JSValue): boolean; assembler;
  1007. // Note: use identity, "==" would fit undefined
  1008. asm
  1009. return v === null;
  1010. end;
  1011. function isRecord(const v: JSValue): boolean; assembler;
  1012. asm
  1013. return (typeof(v)==="object")
  1014. && (typeof(v.$new)==="function")
  1015. && (typeof(v.$clone)==="function")
  1016. && (typeof(v.$eq)==="function")
  1017. && (typeof(v.$assign)==="function");
  1018. end;
  1019. function isUndefined(const v: JSValue): boolean; assembler;
  1020. asm
  1021. return v == undefined;
  1022. end;
  1023. function isDefined(const v: JSValue): boolean; assembler;
  1024. asm
  1025. return !(v == undefined);
  1026. end;
  1027. function isUTF16Char(const v: JSValue): boolean; assembler;
  1028. asm
  1029. if (typeof(v)!="string") return false;
  1030. if ((v.length==0) || (v.length>2)) return false;
  1031. var code = v.charCodeAt(0);
  1032. if (code < 0xD800){
  1033. if (v.length == 1) return true;
  1034. } else if (code <= 0xDBFF){
  1035. if (v.length==2){
  1036. code = v.charCodeAt(1);
  1037. if (code >= 0xDC00 && code <= 0xDFFF) return true;
  1038. };
  1039. };
  1040. return false;
  1041. end;
  1042. function jsInstanceOf(const aFunction, aFunctionWithPrototype: JSValue
  1043. ): Boolean; assembler;
  1044. asm
  1045. return aFunction instanceof aFunctionWithPrototype;
  1046. end;
  1047. function toNumber(const v: JSValue): double; assembler;
  1048. asm
  1049. return v-0;
  1050. end;
  1051. function toInteger(const v: JSValue): NativeInt;
  1052. begin
  1053. if IsInteger(v) then
  1054. Result:=NativeInt(v)
  1055. else
  1056. Result:=0;
  1057. end;
  1058. function toObject(Value: JSValue): TJSObject;
  1059. begin
  1060. if IsObject(Value) then
  1061. Result:=TJSObject(Value)
  1062. else
  1063. Result:=Nil;
  1064. end;
  1065. function toArray(Value: JSValue): TJSArray; // If not possible, returns Nil
  1066. begin
  1067. if IsArray(Value) then
  1068. Result:=TJSArray(Value)
  1069. else
  1070. Result:=Nil;
  1071. end;
  1072. function toBoolean(Value: JSValue): Boolean; // If not possible, returns False
  1073. begin
  1074. if isBoolean(Value) then
  1075. Result:=Boolean(Value)
  1076. else
  1077. Result:=False;
  1078. end;
  1079. function toString(Value: JSValue): String; // If not possible, returns ''
  1080. begin
  1081. if IsString(Value) then
  1082. Result:=String(Value)
  1083. else
  1084. Result:='';
  1085. end;
  1086. { EJS }
  1087. constructor EJS.Create(const Msg: String);
  1088. begin
  1089. FMessage:=Msg;
  1090. end;
  1091. function GetValueType(JS: JSValue): TJSValueType;
  1092. Var
  1093. t : string;
  1094. begin
  1095. if isNull(js) then // null reported as object
  1096. result:=jvtNull
  1097. else
  1098. begin
  1099. t:=jsTypeOf(js);
  1100. if (t='string') then
  1101. Result:=jvtString
  1102. else if (t='boolean') then
  1103. Result:=jvtBoolean
  1104. else if (t='object') then
  1105. begin
  1106. if IsArray(JS) then
  1107. Result:=jvtArray
  1108. else
  1109. Result:=jvtObject;
  1110. end
  1111. else if (t='number') then
  1112. if isInteger(JS) then
  1113. result:=jvtInteger
  1114. else
  1115. result:=jvtFloat
  1116. end;
  1117. end;
  1118. end.