js.pas 47 KB

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