js.pas 54 KB

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