summaryrefslogtreecommitdiff
path: root/packages/fcl-js/src/jsbase.pp
blob: c70cb427b8de3980f2946cacbd448c64dca8aa9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
{ ********************************************************************* 
    This file is part of the Free Component Library (FCL)
    Copyright (c) 2016 Michael Van Canneyt.
       
    Javascript base definitions
            
    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.
                   
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
                                
  **********************************************************************}
                                 
unit jsbase;

{$mode objfpc}{$H+}

interface

{$ifdef pas2js}
uses js;
{$endif}

const
  MinSafeIntDouble = -$1fffffffffffff; // -9007199254740991 53 bits (52 explicitly stored)
  MaxSafeIntDouble =  $1fffffffffffff; //  9007199254740991
Type
  TJSType = (jstUNDEFINED,jstNull,jstBoolean,jstNumber,jstString,jstObject,jstReference,jstCompletion);

  TJSString = UnicodeString;
  TJSChar = WideChar;
  TJSNumber = Double;
  {$ifdef fpc}
  TJSPChar = PWideChar;
  {$endif}

  { TJSValue }

  TJSValue = Class(TObject)
  private
    FValueType: TJSType;
    {$ifdef pas2js}
    FValue: JSValue;
    {$else}
    FValue : Record
      Case Integer of
      0 : (P : Pointer);
      1 : (F : TJSNumber);
      2 : (I : Integer);
    end;
    {$endif}
    FCustomValue: TJSString;
    procedure ClearValue(ANewValue: TJSType);
    function GetAsBoolean: Boolean;
    function GetAsCompletion: TObject;
    function GetAsNumber: TJSNumber;
    function GetAsObject: TObject;
    function GetAsReference: TObject;
    function GetAsString: TJSString;
    function GetIsNull: Boolean;
    function GetIsUndefined: Boolean;
    procedure SetAsBoolean(const AValue: Boolean);
    procedure SetAsCompletion(const AValue: TObject);
    procedure SetAsNumber(const AValue: TJSNumber);
    procedure SetAsObject(const AValue: TObject);
    procedure SetAsReference(const AValue: TObject);
    procedure SetAsString(const AValue: TJSString);
    procedure SetIsNull(const AValue: Boolean);
    procedure SetIsUndefined(const AValue: Boolean);
  Public
    Constructor Create;
    Constructor CreateNull;
    Constructor Create(ANumber : TJSNumber);
    Constructor Create(ABoolean : Boolean);
    Constructor Create(AString: TJSString);
    Destructor Destroy; override;
    Property ValueType : TJSType Read FValueType;
    Property CustomValue: TJSString Read FCustomValue Write FCustomValue;
    Property IsUndefined : Boolean Read GetIsUndefined Write SetIsUndefined;
    Property IsNull : Boolean Read GetIsNull Write SetIsNull;
    Property AsNumber : TJSNumber Read GetAsNumber Write SetAsNumber;
    Property AsBoolean : Boolean Read GetAsBoolean Write SetAsBoolean;
    Property AsObject : TObject Read GetAsObject Write SetAsObject;
    Property AsString : TJSString Read GetAsString Write SetAsString;
    Property AsReference : TObject Read GetAsReference Write SetAsReference;
    Property AsCompletion : TObject Read GetAsCompletion Write SetAsCompletion;
  end;

function IsValidJSIdentifier(Name: TJSString; AllowEscapeSeq: boolean = false): boolean;
function StrToJSString(const S: String): TJSString; inline;
function JSStringToString(const S: TJSString): String; inline;

implementation

function IsValidJSIdentifier(Name: TJSString; AllowEscapeSeq: boolean): boolean;
{$ifdef pas2js}
const
  HexChars = ['0'..'9','a'..'f','A'..'F'];
var
  p, l, i: Integer;
begin
  Result:=false;
  if Name='' then exit;
  l:=length(Name);
  p:=1;
  while p<=l do
    case Name[p] of
    '0'..'9':
      if p=1 then
        exit
      else
        inc(p);
    'a'..'z','A'..'Z','_','$': inc(p);
    '\':
      begin
      if not AllowEscapeSeq then exit;
      inc(p);
      if p>l then exit;
      if Name[p]='x' then
        begin
        // \x00
        inc(p);
        if (p>l) or not (Name[p] in HexChars) then exit;
        inc(p);
        if (p>l) or not (Name[p] in HexChars) then exit;
        end
      else if Name[p]='u' then
        begin
        inc(p);
        if p>l then exit;
        if Name[p]='{' then
          begin
          // \u{00000}
          i:=0;
          repeat
            inc(p);
            if p>l then exit;
            case Name[p] of
            '}': break;
            '0'..'9': i:=i*16+ord(Name[p])-ord('0');
            'a'..'f': i:=i*16+ord(Name[p])-ord('a')+10;
            'A'..'F': i:=i*16+ord(Name[p])-ord('A')+10;
            else exit;
            end;
            if i>$FFFF then exit;
          until false;
          if (i>=$D800) and (i<$E000) then exit;
          inc(p);
          end
        else
          begin
          // \u0000
          for i:=1 to 4 do
            begin
            inc(p);
            if (p>l) or not (Name[p] in HexChars) then exit;
            end;
          end;
        // ToDo: check for invalid values like #$D800 and #$0041
        end
      else
        exit; // unknown sequence
      end;
    #$200C,#$200D: inc(p); // zero width non-joiner/joiner
    #$00AA..#$2000,
    #$200E..#$D7FF:
      inc(p); // ToDo: only those with ID_START/ID_CONTINUE see https://codepoints.net/search?IDC=1
    #$D800..#$DFFF:
      exit; // double code units are not allowed for JS identifiers
    #$E000..#$FFFF:
      inc(p);
    else
      exit;
    end;
  Result:=true;
end;
{$else}
var
  p: TJSPChar;
  i: Integer;
begin
  Result:=false;
  if Name='' then exit;
  p:=TJSPChar(Name);
  repeat
    case p^ of
    #0:
      if p-TJSPChar(Name)=length(Name) then
        exit(true)
      else
        exit;
    '0'..'9':
      if p=TJSPChar(Name) then
        exit
      else
        inc(p);
    'a'..'z','A'..'Z','_','$': inc(p);
    '\':
      begin
      if not AllowEscapeSeq then exit;
      inc(p);
      if p^='x' then
        begin
        // \x00
        for i:=1 to 2 do
          begin
          inc(p);
          if not (p^ in ['0'..'9','a'..'f','A'..'F']) then exit;
          end;
        end
      else if p^='u' then
        begin
        inc(p);
        if p^='{' then
          begin
          // \u{00000}
          i:=0;
          repeat
            inc(p);
            case p^ of
            '}': break;
            '0'..'9': i:=i*16+ord(p^)-ord('0');
            'a'..'f': i:=i*16+ord(p^)-ord('a')+10;
            'A'..'F': i:=i*16+ord(p^)-ord('A')+10;
            else exit;
            end;
            if i>$FFFF then exit;
          until false;
          if (i>=$D800) and (i<$E000) then exit;
          inc(p);
          end
        else
          begin
          // \u0000
          for i:=1 to 4 do
            begin
            inc(p);
            if not (p^ in ['0'..'9','a'..'f','A'..'F']) then exit;
            end;
          end;
        // ToDo: check for invalid values like #$D800 and #$0041
        end
      else
        exit; // unknown sequence
      end;
    #$200C,#$200D: inc(p); // zero width non-joiner/joiner
    #$00AA..#$2000,
    #$200E..#$D7FF:
      inc(p); // ToDo: only those with ID_START/ID_CONTINUE see https://codepoints.net/search?IDC=1
    #$D800..#$DFFF:
      exit; // double code units are not allowed for JS identifiers
    #$E000..#$FFFF:
      inc(p);
    else
      exit;
    end;
  until false;
end;
{$endif}

function StrToJSString(const S: String): TJSString;
begin
  Result:={$ifdef pas2js}S{$else}UTF8Decode(S){$endif};
end;

function JSStringToString(const S: TJSString): String;
begin
  Result:={$ifdef pas2js}S{$else}UTF8Encode(S){$endif};
end;

{ TJSValue }

function TJSValue.GetAsBoolean: Boolean;
begin
  If (ValueType=jstBoolean) then
    Result:={$ifdef pas2js}boolean(FValue){$else}(FValue.I<>0){$endif}
  else
    Result:=False;
end;

function TJSValue.GetAsCompletion: TObject;
begin
  Result:=TObject(FValue{$ifdef fpc}.P{$endif});
end;

function TJSValue.GetAsNumber: TJSNumber;
begin
  If (ValueType=jstNumber) then
    Result:={$ifdef pas2js}TJSNumber(FValue){$else}FValue.F{$endif}
  else
    Result:=0.0;
end;

function TJSValue.GetAsObject: TObject;
begin
  If (ValueType=jstObject) then
    Result:=TObject(FValue{$ifdef fpc}.P{$endif})
  else
    Result:=nil;
end;

function TJSValue.GetAsReference: TObject;
begin
  If (ValueType=jstReference) then
    Result:=TObject(FValue{$ifdef fpc}.P{$endif})
  else
    Result:=nil;
end;

function TJSValue.GetAsString: TJSString;
begin
  If (ValueType=jstString) then
    Result:=TJSString(FValue{$ifdef fpc}.P{$endif})
  else
    Result:='';
end;

function TJSValue.GetIsNull: Boolean;
begin
  Result:=(ValueType=jstNull);
end;

function TJSValue.GetIsUndefined: Boolean;
begin
  Result:=(fValueType=jstUndefined);
end;

procedure TJSValue.ClearValue(ANewValue : TJSType);

begin
  {$ifdef pas2js}
  Case FValueType of
    jstUNDEFINED: FValue:=JS.Undefined;
    jstString : FValue:='';
    jstNumber : FValue:=0;
    jstBoolean : FValue:=false;
  else
    FValue:=JS.Null;
  end;
  {$else}
  Case FValueType of
    jstString : String(FValue.P):='';
    jstNumber : FValue.F:=0;
  else
    FValue.I:=0;
  end;
  {$endif}
  FValueType:=ANewValue;
  FCustomValue:='';
end;

procedure TJSValue.SetAsBoolean(const AValue: Boolean);
begin
  ClearValue(jstBoolean);
  {$ifdef pas2js}
  FValue:=AValue;
  {$else}
  FValue.I:=Ord(AValue);
  {$endif}
end;

procedure TJSValue.SetAsCompletion(const AValue: TObject);
begin
  ClearValue(jstBoolean);
  FValue{$ifdef fpc}.P{$endif}:=AValue;
end;

procedure TJSValue.SetAsNumber(const AValue: TJSNumber);
begin
  ClearValue(jstNumber);
  FValue{$ifdef fpc}.F{$endif}:=AValue;
end;

procedure TJSValue.SetAsObject(const AValue: TObject);
begin
  ClearValue(jstObject);
  FValue{$ifdef fpc}.P{$endif}:=AVAlue;
end;

procedure TJSValue.SetAsReference(const AValue: TObject);
begin
  ClearValue(jstReference);
  FValue{$ifdef fpc}.P{$endif}:=AVAlue;
end;

procedure TJSValue.SetAsString(const AValue: TJSString);
begin
  ClearValue(jstString);
  {$ifdef pas2js}FValue{$else}TJSString(FValue.P){$endif}:=AValue;
end;

procedure TJSValue.SetIsNull(const AValue: Boolean);
begin
  if AValue then
    ClearValue(jstNull)
  else if IsNull then
    ClearValue(jstUNDEFINED);
end;

procedure TJSValue.SetIsUndefined(const AValue: Boolean);
begin
  if AValue then
    ClearValue(jstUndefined)
  else if IsUndefined then
    ClearValue(jstNull);
end;

constructor TJSValue.CreateNull;
begin
  IsNull:=True;
end;

constructor TJSValue.Create;
begin
  IsUndefined:=True;
end;

constructor TJSValue.Create(ANumber: TJSNumber);
begin
  AsNumber:=ANumber;
end;

constructor TJSValue.Create(ABoolean: Boolean);
begin
  AsBoolean:=ABoolean;
end;

constructor TJSValue.Create(AString: TJSString);
begin
  AsString:=AString;
end;

destructor TJSValue.Destroy;
begin
  ClearValue(jstUndefined);
  inherited Destroy;
end;


end.