js.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. TJSArrayCallBack = function (element : JSValue; index: NativeInt; anArray : TJSArray) : Boolean;
  255. TJSArrayEvent = function (element : JSValue; index: NativeInt; anArray : TJSArray) : Boolean of object;
  256. TJSArrayMapCallBack = function (element : JSValue; index: NativeInt; anArray : TJSArray) : JSValue;
  257. TJSArrayMapEvent = function (element : JSValue; index: NativeInt; anArray : TJSArray) : JSValue of object;
  258. TJSArrayReduceCallBack = function (accumulator, currentValue : JSValue; currentIndex : NativeInt; anArray : TJSArray) : JSValue;
  259. TJSArrayCompareCallBack = function (a,b : JSValue) : NativeInt;
  260. { TJSArray }
  261. TJSArray = Class external name 'Array'
  262. private
  263. function GetElements(Index: NativeInt): JSValue; external name '[]';
  264. procedure SetElements(Index: NativeInt; const AValue: JSValue); external name '[]';
  265. public
  266. flength : integer; external name 'length';
  267. constructor new; overload;
  268. constructor new(aLength : NativeInt); overload;
  269. constructor new(aElement1 : JSValue); varargs; overload;
  270. class function _of() : TJSArray; varargs; external name 'of';
  271. class function isArray(a: JSValue) : Boolean;
  272. {$IFDEF JAVASCRIPT2015}
  273. class function from(a : JSValue) : TJSArray;
  274. {$ENDIF}
  275. function concat(el : JSValue) : TJSArray; varargs;
  276. function copyWithin(aTarget : NativeInt) : TJSarray;overload; // not in IE
  277. function copyWithin(aTarget, aStart : NativeInt) : TJSarray;overload; // not in IE
  278. function copyWithin(aTarget, aStart, aEnd : NativeInt) : TJSarray;overload; // not in IE
  279. Function every(const aCallback : TJSArrayCallBack) : boolean;overload;
  280. Function every(const aCallback : TJSArrayEvent; aThis : TObject) : boolean;overload;
  281. Function filter(const aCallBack : TJSArrayCallBack) : TJSArray; overload;
  282. Function filter(const aCallBack : TJSArrayEvent; aThis : TObject) : TJSArray;overload;
  283. Function fill(aValue : JSValue) : TJSArray; overload;
  284. Function fill(aValue : JSValue; aStartIndex : NativeInt) : TJSArray; overload;
  285. Function fill(aValue : JSValue; aStartIndex,aEndIndex : NativeInt) : TJSArray; overload;
  286. Function find(const aCallBack : TJSArrayCallBack) : JSValue; overload;
  287. Function find(const aCallBack : TJSArrayEvent; aThis : TObject) : JSValue; overload;
  288. Function findIndex(const aCallBack : TJSArrayCallBack) : NativeInt; overload;
  289. Function findIndex(const aCallBack : TJSArrayEvent; aThis : TObject) : NativeInt; overload;
  290. procedure forEach(const aCallBack : TJSArrayCallBack); overload;
  291. procedure forEach(const aCallBack : TJSArrayEvent; aThis : TObject); overload;
  292. function includes(aElement : JSValue) : Boolean; overload;
  293. function includes(aElement : JSValue; FromIndex : NativeInt) : Boolean; overload;
  294. function indexOf(aElement : JSValue) : NativeInt; overload;
  295. function indexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  296. function join : String; overload;
  297. function join (aSeparator : string) : String; overload;
  298. function lastIndexOf(aElement : JSValue) : NativeInt; overload;
  299. function lastIndexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  300. Function map(const aCallBack : TJSArrayCallBack) : TJSArray; overload;
  301. Function map(const aCallBack : TJSArrayEvent; aThis : TObject) : TJSArray; overload;
  302. function pop : JSValue;
  303. function push(aElement : JSValue) : NativeInt; varargs;
  304. function reduce(const aCallBack : TJSArrayReduceCallBack) : JSValue; overload;
  305. function reduce(const aCallBack : TJSArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  306. function reduceRight(const aCallBack : TJSArrayReduceCallBack) : JSValue; overload;
  307. function reduceRight(const aCallBack : TJSArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  308. Function reverse : TJSArray;
  309. Function shift : JSValue;
  310. Function slice : TJSArray; overload;
  311. function slice(aBegin : NativeInt) : TJSArray; overload;
  312. function slice(aBegin,aEnd : NativeInt) : TJSArray; overload;
  313. Function some(const aCallback : TJSArrayCallBack) : boolean; overload;
  314. Function some(const aCallback : TJSArrayEvent; aThis : TObject) : boolean; overload;
  315. Function sort(const aCallback : TJSArrayCompareCallBack) : TJSArray; overload;
  316. Function sort() : TJSArray; overload;
  317. function splice(aStart : NativeInt) : TJSArray; overload;
  318. function splice(aStart,aDeleteCount : NativeInt) : TJSArray; varargs; overload;
  319. function toLocaleString: String; overload;
  320. function toLocaleString(locales : string) : String; overload;
  321. function toLocaleString(locales : string; const Options : TLocaleCompareOptions) : String; overload;
  322. function toString : String;
  323. function unshift : NativeInt; varargs;
  324. Property Length : Integer Read FLength;
  325. property Elements[Index: NativeInt]: JSValue read GetElements write SetElements; default;
  326. end;
  327. TJSArrayBuffer = Class external name 'ArrayBuffer'
  328. private
  329. fLength : NativeInt; external name 'byteLength';
  330. public
  331. constructor new(aByteLength : NativeInt);
  332. class function isView(aValue : JSValue) : Boolean;
  333. function slice(aBegin : NativeInt) : TJSArrayBuffer; overload;
  334. function slice(aBegin,aEnd : NativeInt) : TJSArrayBuffer; overload;
  335. Property byteLength : NativeInt Read fLength;
  336. end;
  337. TJSBufferSource = class external name 'BufferSource'
  338. end;
  339. { TJSTypedArray }
  340. TJSTypedArray = Class;
  341. TJSTypedArrayCallBack = function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : Boolean;
  342. TJSTypedArrayEvent = function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : Boolean of object;
  343. TJSTypedArrayMapCallBack = function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : JSValue;
  344. TJSTypedArrayMapEvent = function (element : JSValue; index: NativeInt; anArray : TJSTypedArray) : JSValue of object;
  345. TJSTypedArrayReduceCallBack = function (accumulator, currentValue : JSValue; currentIndex : NativeInt; anArray : TJSTypedArray) : JSValue;
  346. TJSTypedArrayCompareCallBack = function (a,b : JSValue) : NativeInt;
  347. TJSTypedArray = class external name 'TypedArray' (TJSBufferSource)
  348. Private
  349. FBuffer: TJSArrayBuffer; external name 'buffer';
  350. FByteLength: NativeInt; external name 'byteLength';
  351. FLength: NativeInt; external name 'length';
  352. FByteOffset: NativeInt; external name 'byteOffset';
  353. function getValue(Index : NativeInt) : JSValue; external name '[]';
  354. procedure setValue(Index : NativeInt;AValue : JSValue); external name '[]';
  355. Public
  356. class var BYTES_PER_ELEMENT : NativeInt;
  357. class var name : string;
  358. class function from(aValue : jsValue) : TJSTypedArray;
  359. class function from(aValue : jsValue; Map : TJSTypedArrayMapCallBack) : TJSTypedArray;
  360. class function from(aValue : jsValue; aMap : TJSTypedArrayMapEvent) : TJSTypedArray;
  361. class function _of(aValue : jsValue) : TJSTypedArray; varargs;
  362. function copyWithin(aTarget : NativeInt) : TJSTypedArray;overload;
  363. function copyWithin(aTarget, aStart : NativeInt) : TJSTypedArray;overload;
  364. function copyWithin(aTarget, aStart, aEnd : NativeInt) : TJSTypedArray;overload;
  365. Function every(const aCallback : TJSTypedArrayCallBack) : boolean;overload;
  366. Function every(const aCallback : TJSTypedArrayEvent; aThis : TObject) : boolean;overload;
  367. Function fill(aValue : JSValue) : TJSTypedArray; overload;
  368. Function fill(aValue : JSValue; aStartIndex : NativeInt) : TJSTypedArray; overload;
  369. Function fill(aValue : JSValue; aStartIndex,aEndIndex : NativeInt) : TJSTypedArray; overload;
  370. Function filter(const aCallBack : TJSTypedArrayCallBack) : TJSTypedArray; overload;
  371. Function filter(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : TJSTypedArray;overload;
  372. Function find(const aCallBack : TJSTypedArrayCallBack) : JSValue; overload;
  373. Function find(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : JSValue; overload;
  374. Function findIndex(const aCallBack : TJSTypedArrayCallBack) : NativeInt; overload;
  375. Function findIndex(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : NativeInt; overload;
  376. procedure forEach(const aCallBack : TJSTypedArrayCallBack); overload;
  377. procedure forEach(const aCallBack : TJSTypedArrayEvent; aThis : TObject); overload;
  378. function includes(aElement : JSValue) : Boolean; overload;
  379. function includes(aElement : JSValue; FromIndex : NativeInt) : Boolean; overload;
  380. function indexOf(aElement : JSValue) : NativeInt; overload;
  381. function indexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  382. function join : String; overload;
  383. function join (aSeparator : string) : String; overload;
  384. function lastIndexOf(aElement : JSValue) : NativeInt; overload;
  385. function lastIndexOf(aElement : JSValue; FromIndex : NativeInt) : NativeInt; overload;
  386. Function map(const aCallBack : TJSTypedArrayCallBack) : TJSTypedArray; overload;
  387. Function map(const aCallBack : TJSTypedArrayEvent; aThis : TObject) : TJSTypedArray; overload;
  388. function reduce(const aCallBack : TJSTypedArrayReduceCallBack) : JSValue; overload;
  389. function reduce(const aCallBack : TJSTypedArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  390. function reduceRight(const aCallBack : TJSTypedArrayReduceCallBack) : JSValue; overload;
  391. function reduceRight(const aCallBack : TJSTypedArrayReduceCallBack; initialValue : JSValue) : JSValue; overload;
  392. Function reverse : TJSTypedArray;
  393. procedure _set(anArray : TJSTypedArray);
  394. procedure _set(anArray : TJSTypedArray; anOffset : NativeInt);
  395. Function slice : TJSTypedArray; overload;
  396. function slice(aBegin : NativeInt) : TJSTypedArray; overload;
  397. function slice(aBegin,aEnd : NativeInt) : TJSTypedArray; overload;
  398. Function some(const aCallback : TJSTypedArrayCallBack) : boolean; overload;
  399. Function some(const aCallback : TJSTypedArrayEvent; aThis : TObject) : boolean; overload;
  400. Function sort(const aCallback : TJSTypedArrayCompareCallBack) : TJSTypedArray; overload;
  401. Function sort() : TJSTypedArray; overload;
  402. function splice(aStart : NativeInt) : TJSTypedArray; overload;
  403. function splice(aStart,aDeleteCount : NativeInt) : TJSTypedArray; varargs; overload;
  404. function toLocaleString: String; overload;
  405. function toLocaleString(locales : string) : String; overload;
  406. function toLocaleString(locales : string; const Options : TLocaleCompareOptions) : String; overload;
  407. function toString : String;
  408. function unshift : NativeInt; varargs;
  409. property buffer : TJSArrayBuffer read FBuffer;
  410. property byteLength : NativeInt Read FByteLength;
  411. property byteOffset : NativeInt Read FByteOffset;
  412. property length : NativeInt Read FLength;
  413. Property values[Index : NativeInt] : JSValue Read getValue Write SetValue; default;
  414. end;
  415. { TJSInt8Array }
  416. TJSInt8Array = class external name 'Int8Array' (TJSTypedArray)
  417. private
  418. function getTypedValue(Index : Integer): Shortint; external name '[]';
  419. procedure setTypedValue(Index : Integer; AValue: Shortint);external name '[]';
  420. public
  421. Property values[Index : Integer] : Shortint Read getTypedValue Write setTypedValue; default;
  422. end;
  423. TJSUint8Array = class external name 'UInt8Array' (TJSTypedArray)
  424. private
  425. function getTypedValue(Index : Integer): Byte; external name '[]';
  426. procedure setTypedValue(Index : Integer; AValue: Byte);external name '[]';
  427. public
  428. Property values[Index : Integer] : Byte Read getTypedValue Write setTypedValue; default;
  429. end;
  430. TJSUint8ClampedArray = class external name 'UInt8ClampedArray' (TJSTypedArray)
  431. private
  432. function getTypedValue(Index : Integer): Byte; external name '[]';
  433. procedure setTypedValue(Index : Integer; AValue: Byte);external name '[]';
  434. public
  435. Property values[Index : Integer] : Byte Read getTypedValue Write setTypedValue; default;
  436. end;
  437. TJSInt16Array = class external name 'Int16Array' (TJSTypedArray)
  438. private
  439. function getTypedValue(Index : Integer): smallint; external name '[]';
  440. procedure setTypedValue(Index : Integer; AValue: Smallint);external name '[]';
  441. public
  442. Property values[Index : Integer] : SmallInt Read getTypedValue Write setTypedValue; default;
  443. end;
  444. TJSUint16Array = class external name 'UInt16Array' (TJSTypedArray)
  445. private
  446. function getTypedValue(Index : Integer): Word; external name '[]';
  447. procedure setTypedValue(Index : Integer; AValue: Word);external name '[]';
  448. public
  449. Property values[Index : Integer] : Word Read getTypedValue Write setTypedValue; default;
  450. end;
  451. TJSInt32Array = class external name 'Int32Array' (TJSTypedArray)
  452. private
  453. function getTypedValue(Index : Integer): longint; external name '[]';
  454. procedure setTypedValue(Index : Integer; AValue: longint);external name '[]';
  455. public
  456. Property values[Index : Integer] : longint Read getTypedValue Write setTypedValue; default;
  457. end;
  458. TJSUint32Array = class external name 'UInt32Array' (TJSTypedArray)
  459. private
  460. function getTypedValue(Index : Integer): LongWord; external name '[]';
  461. procedure setTypedValue(Index : Integer; AValue: LongWord);external name '[]';
  462. public
  463. Property values[Index : Integer] : LongWord Read getTypedValue Write setTypedValue; default;
  464. end;
  465. TJSFloat32Array = class external name 'Float32Array' (TJSTypedArray)
  466. private
  467. function getTypedValue(Index : Integer): Float32; external name '[]';
  468. procedure setTypedValue(Index : Integer; AValue: Float32);external name '[]';
  469. public
  470. Property values[Index : Integer] : Float32 Read getTypedValue Write setTypedValue; default;
  471. end;
  472. TJSFloat64Array = class external name 'Float64Array' (TJSTypedArray)
  473. private
  474. function getTypedValue(Index : Integer): Float64; external name '[]';
  475. procedure setTypedValue(Index : Integer; AValue: Float64);external name '[]';
  476. public
  477. Property values[Index : Integer] : Float64 Read getTypedValue Write setTypedValue; default;
  478. end;
  479. TJSDataView = Class external name 'DataView' (TJSBufferSource)
  480. private
  481. fBuffer : TJSArrayBuffer; external name 'buffer';
  482. fLength : NativeInt; external name 'byteLength';
  483. fOffset : NativeInt; external name 'byteOffset';
  484. public
  485. constructor new(aBuffer : TJSArrayBuffer); overload;
  486. constructor new(aBuffer : TJSArrayBuffer; aOffset : NativeInt); overload;
  487. constructor new(aBuffer : TJSArrayBuffer; aOffset,aByteLength : NativeInt); overload;
  488. function getFloat32(aByteOffset : NativeInt) : double; overload;
  489. function getFloat32(aByteOffset : NativeInt; aLittleEndian: Boolean) : double; overload;
  490. function getFloat64(aByteOffset : NativeInt) : double; overload;
  491. function getFloat64(aByteOffset : NativeInt; aLittleEndian: Boolean) : double; overload;
  492. function getInt8(aByteOffset : NativeInt) : ShortInt;
  493. function getInt16(aByteOffset : NativeInt) : SmallInt; overload;
  494. function getInt16(aByteOffset : NativeInt; aLittleEndian : Boolean) : SmallInt; overload;
  495. function getInt32(aByteOffset : NativeInt) : Longint; overload;
  496. function getInt32(aByteOffset : NativeInt; aLittleEndian : Boolean) : Longint; overload;
  497. function getUint8(aByteOffset : NativeInt) : Byte; overload;
  498. function getUint16(aByteOffset : NativeInt) : Word; overload;
  499. function getUint16(aByteOffset : NativeInt; aLittleEndian : Boolean) : Word; overload;
  500. function getUint32(aByteOffset : NativeInt) : LongWord; overload;
  501. function getUint32(aByteOffset : NativeInt; aLittleEndian : Boolean) : LongWord; overload;
  502. procedure setFloat32(aByteOffset : NativeInt; aValue : double); overload;
  503. procedure setFloat32(aByteOffset : NativeInt; aValue : double; aLittleEndian: Boolean); overload;
  504. procedure setFloat64(aByteOffset : NativeInt; aValue : double); overload;
  505. procedure setFloat64(aByteOffset : NativeInt; aValue : double; aLittleEndian: Boolean); overload;
  506. procedure setInt8(aByteOffset : NativeInt; aValue : ShortInt);
  507. procedure setInt16(aByteOffset : NativeInt; aValue : SmallInt); overload;
  508. procedure setInt16(aByteOffset : NativeInt; aValue : SmallInt; aLittleEndian : Boolean); overload;
  509. procedure setInt32(aByteOffset : NativeInt; aValue : Longint); overload;
  510. procedure setInt32(aByteOffset : NativeInt; aValue : Longint; aLittleEndian : Boolean); overload;
  511. procedure setUint8(aByteOffset : NativeInt; aValue : Byte); overload;
  512. procedure setUint16(aByteOffset : NativeInt; aValue : Word); overload;
  513. procedure setUint16(aByteOffset : NativeInt; aValue : Word; aLittleEndian : Boolean); overload;
  514. procedure setUint32(aByteOffset : NativeInt; aValue : LongWord); overload;
  515. procedure setUint32(aByteOffset : NativeInt; aValue: LongWord; aLittleEndian : Boolean); overload;
  516. Property byteLength : NativeInt Read fLength;
  517. Property byteOffset : NativeInt read fOffset;
  518. property buffer : TJSArrayBuffer Read fBuffer;
  519. end;
  520. TJSJSON = class external name 'JSON' (TJSObject)
  521. Public
  522. class function parse(aJSON : String) : TJSObject;
  523. class function stringify(aValue : JSValue) : string;
  524. class function stringify(aValue,aReplacer : JSValue) : string;
  525. class function stringify(aValue,aReplacer : JSValue; space: NativeInt) : string;
  526. class function stringify(aValue,aReplacer : JSValue; space: String) : string;
  527. end;
  528. var
  529. // This can be used in procedures/functions to provide access to the 'arguments' array.
  530. JSArguments: TJSValueDynArray; external name 'arguments';
  531. // This can be used in all code to access the javascript 'this' object.
  532. JSThis: TJSObject; external name 'this';
  533. // This can be used in catch blocks to access the JS throw value
  534. JSExceptValue: JSValue; external name '$e';
  535. Function new(aElements: TJSValueDynArray) : TJSObject;
  536. function decodeURIComponent(encodedURI : String) : String; external name 'decodeURIComponent';
  537. function encodeURIComponent(str : String) : String; external name 'encodeURIComponent';
  538. function parseInt(s: String; Radix: NativeInt): NativeInt; overload; external name 'parseInt'; // may result NaN
  539. function parseInt(s: String): NativeInt; overload; external name 'parseInt'; // may result NaN
  540. function parseFloat(s: String): double; overload; external name 'parseFloat'; // may result NaN
  541. function hasString(const v: JSValue): boolean; external name 'rtl.hasString';// isString(v) and v<>''
  542. 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.
  543. function isArray(const v: JSValue): boolean; external name 'rtl.isArray';
  544. function isBoolean(const v: JSValue): boolean; assembler;
  545. function isCallback(const v: JSValue): boolean; assembler;
  546. function isChar(const v: JSValue): boolean; assembler;
  547. function isClass(const v: JSValue): boolean; assembler; // is a Pascal class, e.g. a TClass
  548. function isClassInstance(const v: JSValue): boolean; assembler;// is a Pascal class instance, e.g. a TObject
  549. function isFunction(const v: JSValue): boolean; external name 'rtl.isFunction';
  550. function isInteger(const v: JSValue): boolean; assembler;
  551. function isModule(const v: JSValue): boolean; external name 'rtl.isModule';
  552. function isNull(const v: JSValue): boolean; assembler;
  553. function isNumber(const v: JSValue): boolean; external name 'rtl.isNumber';
  554. function isObject(const v: JSValue): boolean; external name 'rtl.isObject'; // true if not null and a JS Object
  555. function isRecord(const v: JSValue): boolean; assembler;
  556. function isString(const v: JSValue): boolean; external name 'rtl.isString';
  557. function isUndefined(const v: JSValue): boolean; assembler;
  558. function isDefined(const v: JSValue): boolean; assembler;
  559. function isUTF16Char(const v: JSValue): boolean; assembler;
  560. function isExt(const InstanceOrClass, aClass: JSValue): boolean; external name 'rtl.isExt'; // aClass can be a JS object or function
  561. function jsInstanceOf(const aFunction, aFunctionWithPrototype: JSValue): String; assembler;
  562. function jsTypeOf(const v: JSValue): String; external name 'typeof';
  563. 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'
  564. function toNumber(const v: JSValue): double; assembler; // if not possible, returns NaN
  565. Type
  566. TJSValueType = (jvtNull,jvtBoolean,jvtInteger,jvtFloat,jvtString,jvtObject,jvtArray);
  567. Function GetValueType(JS : JSValue) : TJSValueType;
  568. Var
  569. Null : JSValue; external name 'null';
  570. Undefined : JSValue; external name 'undefined';
  571. implementation
  572. Function new(aElements: TJSValueDynArray) : TJSObject;
  573. function toString(I : Integer): string; external name 'String';
  574. Var
  575. L,I : integer;
  576. S : String;
  577. begin
  578. L:=length(aElements);
  579. if (L mod 2)=1 then
  580. raise EJS.Create('Number of arguments must be even');
  581. I:=0;
  582. // Check all arguments;
  583. While (i<L) do
  584. begin
  585. if Not isString(aElements[i]) then
  586. begin
  587. S:=ToString(I);
  588. raise EJS.Create('Argument '+S+' must be a string.');
  589. end;
  590. inc(I,2);
  591. end;
  592. I:=0;
  593. Result:=TJSObject.New;
  594. While (i<L) do
  595. begin
  596. S:=String(aElements[i]);
  597. Result.Properties[S]:=aElements[i+1];
  598. inc(I,2);
  599. end;
  600. end;
  601. function hasValue(const v: JSValue): boolean; assembler;
  602. asm
  603. if(v){ return true; } else { return false; };
  604. end;
  605. function isBoolean(const v: JSValue): boolean; assembler;
  606. asm
  607. return typeof(v) == 'boolean';
  608. end;
  609. function isCallback(const v: JSValue): boolean; assembler;
  610. asm
  611. return rtl.isObject(v) && rtl.isObject(v.scope) && (rtl.isString(v.fn) || rtl.isFunction(v.fn));
  612. end;
  613. function isChar(const v: JSValue): boolean; assembler;
  614. asm
  615. return (typeof(v)!="string") && (v.length==1);
  616. end;
  617. function isClass(const v: JSValue): boolean; assembler;
  618. asm
  619. return (typeof(v)=="object") && (v!=null) && (v.$class == v);
  620. end;
  621. function isClassInstance(const v: JSValue): boolean; assembler;
  622. asm
  623. return (typeof(v)=="object") && (v!=null) && (v.$class == Object.getPrototypeOf(v));
  624. end;
  625. function isInteger(const v: JSValue): boolean; assembler;
  626. asm
  627. return Math.floor(v)===v;
  628. end;
  629. function isNull(const v: JSValue): boolean; assembler;
  630. // Note: use identity, "==" would fit undefined
  631. asm
  632. return v === null;
  633. end;
  634. function isRecord(const v: JSValue): boolean; assembler;
  635. asm
  636. return (typeof(v)=="function") && (typeof(v.$create) == "function");
  637. end;
  638. function isUndefined(const v: JSValue): boolean; assembler;
  639. asm
  640. return v == undefined;
  641. end;
  642. function isDefined(const v: JSValue): boolean; assembler;
  643. asm
  644. return !(v == undefined);
  645. end;
  646. function isUTF16Char(const v: JSValue): boolean; assembler;
  647. asm
  648. if (typeof(v)!="string") return false;
  649. if ((v.length==0) || (v.length>2)) return false;
  650. var code = v.charCodeAt(0);
  651. if (code < 0xD800){
  652. if (v.length == 1) return true;
  653. } else if (code <= 0xDBFF){
  654. if (v.length==2){
  655. code = v.charCodeAt(1);
  656. if (code >= 0xDC00 && code <= 0xDFFF) return true;
  657. };
  658. };
  659. return false;
  660. end;
  661. function jsInstanceOf(const aFunction, aFunctionWithPrototype: JSValue
  662. ): String; assembler;
  663. asm
  664. return aFunction instanceof aFunctionWithPrototype;
  665. end;
  666. function toNumber(const v: JSValue): double; assembler;
  667. asm
  668. return v-0;
  669. end;
  670. { EJS }
  671. constructor EJS.Create(const Msg: String);
  672. begin
  673. FMessage:=Msg;
  674. end;
  675. Function GetValueType(JS : JSValue) : TJSValueType;
  676. Var
  677. t : string;
  678. begin
  679. if isNull(js) then // null reported as object
  680. result:=jvtNull
  681. else
  682. begin
  683. t:=jsTypeOf(js);
  684. if (t='string') then
  685. Result:=jvtString
  686. else if (t='boolean') then
  687. Result:=jvtBoolean
  688. else if (t='object') then
  689. begin
  690. if IsArray(JS) then
  691. Result:=jvtArray
  692. else
  693. Result:=jvtObject;
  694. end
  695. else if (t='number') then
  696. if isInteger(JS) then
  697. result:=jvtInteger
  698. else
  699. result:=jvtFloat
  700. end;
  701. end;
  702. end.