js.pas 52 KB

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