js.pas 54 KB

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