js.pas 52 KB

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