js.pas 48 KB

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