-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathdwsJSONScript.pas
More file actions
522 lines (464 loc) · 17.6 KB
/
dwsJSONScript.pas
File metadata and controls
522 lines (464 loc) · 17.6 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
{**********************************************************************}
{ }
{ "The contents of this file are subject to the Mozilla Public }
{ License Version 1.1 (the "License"); you may not use this }
{ file except in compliance with the License. You may obtain }
{ a copy of the License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an }
{ "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express }
{ or implied. See the License for the specific language }
{ governing rights and limitations under the License. }
{ }
{ Copyright Creative IT. }
{ Current maintainer: Eric Grange }
{ }
{**********************************************************************}
unit dwsJSONScript;
{$I dws.inc}
interface
uses
dwsJSON, dwsExprList, dwsDataContext, dwsSymbols, dwsExprs, dwsScriptSource,
dwsDynamicArrays, dwsAssociativeArrays;
type
// JSONScript
//
JSONScript = class {static sealed}
class procedure StringifyExpr(expr : TTypedExpr; exec : TdwsExecution; writer : TdwsJSONWriter); static;
class procedure StringifyArgs(const args : TExprBaseListExec; var Result : String); static;
class procedure PrettyStringifyArgs(const args : TExprBaseListExec; var Result : String); static;
class procedure StringifyVariant(exec : TdwsExecution; writer : TdwsJSONWriter; const v : Variant); static;
class procedure StringifyUnknown(exec : TdwsExecution; writer : TdwsJSONWriter;
const unk : IUnknown); static;
class procedure StringifySymbol(exec : TdwsExecution; writer : TdwsJSONWriter;
sym : TSymbol; const dataPtr : IDataContext); static;
class procedure StringifyDynamicArray(exec : TdwsExecution; writer : TdwsJSONWriter;
const dynArray : IScriptDynArray); static;
class procedure StringifyDataContextArray(exec : TdwsExecution; writer : TdwsJSONWriter; elemSym : TTypeSymbol;
const dataPtr : IDataContext; nb : Integer); static;
class procedure StringifyAssociativeArray(exec : TdwsExecution; writer : TdwsJSONWriter;
assocArray : TScriptAssociativeArray); static;
class procedure StringifyComposite(exec : TdwsExecution; writer : TdwsJSONWriter;
compSym : TCompositeTypeSymbol;
const dataPtr : IDataContext); static;
class procedure StringifyClass(exec : TdwsExecution; writer : TdwsJSONWriter;
const obj : IScriptObj); static;
class procedure StringifySetOf(exec : TdwsExecution; writer : TdwsJSONWriter; setOf : TSetOfSymbol;
const dataPtr : IDataContext); static;
end;
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
uses
dwsUtils, dwsCompilerUtils, dwsConstExprs, dwsArrayElementContext, dwsStrings,
dwsInfo, dwsInfoClasses, dwsStack;
// ------------------
// ------------------ JSONScript ------------------
// ------------------
// StringifyExpr
//
class procedure JSONScript.StringifyExpr(expr : TTypedExpr; exec : TdwsExecution; writer : TdwsJSONWriter);
var
dataExpr : TDataExpr;
v : Variant;
dc : IDataContext;
typ : TTypeSymbol;
begin
typ := expr.Typ.UnAliasedType;
if (typ.Size=1) and (typ.ClassType <> TSetOfSymbol) then begin
expr.EvalAsVariant(exec, v);
if (typ.ClassType = TRecordSymbol) or typ.InheritsFrom(TStaticArraySymbol) then begin
exec.DataContext_CreateValue(v, dc);
StringifySymbol(exec, writer, expr.Typ, dc);
end else StringifyVariant(exec, writer, v);
end else begin
dataExpr:=(expr as TDataExpr);
StringifySymbol(exec, writer, expr.Typ, dataExpr.DataPtr[exec]);
end;
end;
// StringifyArgs
//
class procedure JSONScript.StringifyArgs(const args : TExprBaseListExec; var Result : String);
var
writer : TdwsJSONWriter;
expr : TTypedExpr;
begin
writer:=TdwsJSONWriter.Create;
try
expr:=(args.ExprBase[0] as TTypedExpr);
StringifyExpr(expr, args.Exec, writer);
writer.StoreToUnicodeString(Result);
finally
writer.Free;
end;
end;
// PrettyStringifyArgs
//
class procedure JSONScript.PrettyStringifyArgs(const args : TExprBaseListExec; var Result : String);
var
writer : TdwsJSONWriter;
expr : TTypedExpr;
begin
writer := TdwsJSONBeautifiedWriter.Create(nil, 0, 1, args.AsString[1]);
try
expr:=(args.ExprBase[0] as TTypedExpr);
StringifyExpr(expr, args.Exec, writer);
writer.StoreToUnicodeString(Result);
finally
writer.Free;
end;
end;
// StringifyVariant
//
class procedure JSONScript.StringifyVariant(exec : TdwsExecution; writer : TdwsJSONWriter; const v : Variant);
procedure StringifyString(writer : TdwsJSONWriter; const v : Variant);
var
s : UnicodeString;
begin
VariantToString(v, s);
writer.WriteString(s);
end;
var
p : PVarData;
begin
p:=PVarData(@v);
case p^.VType of
varUString :
writer.WriteStringP(Pointer(p^.VUString), Length(UnicodeString(p.VUString)));
varInt64 :
writer.WriteInteger(p^.VInt64);
varDouble :
writer.WriteNumber(p^.VDouble);
varBoolean :
writer.WriteBoolean(p^.VBoolean);
varNull :
writer.WriteNull;
varUnknown :
if p^.VUnknown=nil then
writer.WriteNull
else StringifyUnknown(exec, writer, IUnknown(p^.VUnknown));
else
StringifyString(writer, v);
end;
end;
// StringifyUnknown
//
class procedure JSONScript.StringifyUnknown(exec : TdwsExecution; writer : TdwsJSONWriter; const unk : IUnknown);
procedure StringifyIGetSelf(const getSelf : IGetSelf);
var
selfObj : TObject;
scriptObj : IScriptObj;
begin
selfObj := getSelf.GetSelf;
if selfObj is TScriptObjInstance then begin
scriptObj := TScriptObjInstance(selfObj);
StringifyClass(exec, writer, scriptObj);
end else if selfObj is TScriptAssociativeArray then begin
StringifyAssociativeArray(exec, writer, TScriptAssociativeArray(selfObj))
end else if selfObj <> nil then begin
writer.WriteString(selfObj.ToString)
end else writer.WriteString('null');
end;
var
intf : IInterface;
begin
if unk = nil then
writer.WriteNull
else if unk.QueryInterface(IJSONWriteAble, intf) = S_OK then begin
IJSONWriteAble(intf).WriteToJSON(writer);
end else if unk.QueryInterface(IScriptDynArray, intf) = S_OK then begin
StringifyDynamicArray(exec, writer, IScriptDynArray(intf));
end else if unk.QueryInterface(IGetSelf, intf) = S_OK then begin
StringifyIGetSelf(IGetSelf(intf));
end else writer.WriteString('IUnknown');
end;
// StringifySymbol
//
class procedure JSONScript.StringifySymbol(exec : TdwsExecution; writer : TdwsJSONWriter; sym : TSymbol; const dataPtr : IDataContext);
procedure DoBaseSymbol;
begin
StringifyVariant(exec, writer, dataPtr[0])
end;
procedure DoDynamicArray;
begin
StringifyDynamicArray(exec, writer, IScriptDynArray(dataPtr.AsInterface[0]))
end;
procedure DoAssociativeArray;
begin
StringifyAssociativeArray(exec, writer, IScriptAssociativeArray(dataPtr.AsInterface[0]).GetSelf as TScriptAssociativeArray)
end;
procedure DoClassSymbol;
begin
StringifyClass(exec, writer, IScriptObj(dataPtr.AsInterface[0]))
end;
procedure DoSetOfSymbol;
begin
StringifySetOf(exec, writer, TSetOfSymbol(sym), dataPtr);
end;
procedure DoFallback;
begin
writer.WriteString(sym.ClassName);
end;
var
ct : TClass;
begin
sym := sym.BaseType;
if sym.IsBaseType then
DoBaseSymbol
else begin
ct := sym.ClassType;
if ct = TDynamicArraySymbol then
DoDynamicArray
else if ct = TRecordSymbol then
StringifyComposite(exec, writer, TRecordSymbol(sym), dataPtr)
else if ct = TClassSymbol then
DoClassSymbol
else if ct = TAssociativeArraySymbol then
DoAssociativeArray
else if ct = TNilSymbol then
writer.WriteNull
else if ct = TSetOfSymbol then
DoSetOfSymbol
else if ct.InheritsFrom(TStaticArraySymbol) then
StringifyDataContextArray(exec, writer, TStaticArraySymbol(sym).Typ, dataPtr, TStaticArraySymbol(sym).ElementCount)
else DoFallback;
end;
end;
// StringifyDataContextArray
//
class procedure JSONScript.StringifyDataContextArray(exec : TdwsExecution;
writer : TdwsJSONWriter; elemSym : TTypeSymbol; const dataPtr : IDataContext; nb : Integer);
procedure HandleBaseString;
var
i : Integer;
s : String;
begin
for i:=0 to nb-1 do begin
dataPtr.EvalAsString(i, s);
writer.WriteString(s);
end;
end;
procedure HandleDefault;
var
i, s : Integer;
locData : IDataContext;
begin
s := elemSym.Size;
for i := 0 to nb-1 do begin
dataPtr.CreateOffset(i*s, locData);
StringifySymbol(exec, writer, elemSym, locData);
end;
end;
var
i : Integer;
unaliasedClassType : TClass;
begin
writer.BeginArray;
unaliasedClassType := elemSym.UnAliasedType.ClassType;
if unaliasedClassType = TBaseIntegerSymbol then begin
for i := 0 to nb-1 do
writer.WriteInteger(dataPtr.AsInteger[i]);
end else if unaliasedClassType = TBaseFloatSymbol then begin
for i := 0 to nb-1 do
writer.WriteNumber(dataPtr.AsFloat[i]);
end else if unaliasedClassType=TBaseStringSymbol then
HandleBaseString
else HandleDefault;
writer.EndArray;
end;
// StringifyDynamicArray
//
class procedure JSONScript.StringifyDynamicArray(exec : TdwsExecution;
writer : TdwsJSONWriter; const dynArray : IScriptDynArray);
var
locData : IDataContext;
writeable : IJSONWriteAble;
dynData : TScriptDynamicDataArray;
elementType : TTypeSymbol;
i : Integer;
begin
if dynArray.QueryInterface(IJSONWriteAble, writeable) = S_OK then
writeable.WriteToJSON(writer)
else begin
elementType := dynArray.elementType;
if dynArray.GetSelf is TScriptDynamicDataArray then begin
dynData := dynArray.GetSelf as TScriptDynamicDataArray;
exec.DataContext_CreateOffset(dynData, 0, locData);
StringifyDataContextArray(exec, writer, elementType, locData, dynData.ArrayLength);
end else begin
writer.BeginArray;
for i := 0 to dynArray.ArrayLength-1 do begin
locData := TArrayElementDataContext.Create(dynArray, i);
StringifySymbol(exec, writer, elementType, locData);
end;
writer.EndArray;
end;
end;
end;
// StringifyAssociativeArray
//
class procedure JSONScript.StringifyAssociativeArray(exec : TdwsExecution;
writer : TdwsJSONWriter; assocArray : TScriptAssociativeArray);
var
i : Integer;
key : TData;
elementData : IDataContext;
name : UnicodeString;
begin
writer.BeginObject;
if (assocArray.Count>0) and assocArray.KeyType.UnAliasedTypeIs(TBaseSymbol) then begin
SetLength(key, assocArray.KeyType.Size);
for i := 0 to assocArray.Capacity-1 do begin
if assocArray.ReadBucket(i, key, elementData) then begin
VariantToString(key[0], name);
writer.WriteName(name);
StringifySymbol(exec, writer, assocArray.ElementType, elementData);
end;
end;
end;
writer.EndObject;
end;
// StringifyComposite
//
class procedure JSONScript.StringifyComposite(exec : TdwsExecution;
writer : TdwsJSONWriter; compSym : TCompositeTypeSymbol; const dataPtr : IDataContext);
var
progInfo : TProgramInfo;
memberSymbol : TSymbol;
procedure DoGetter;
var
info : IInfo;
scriptObj : IScriptObj;
begin
Assert(progInfo <> nil);
if compSym.ClassType = TRecordSymbol then begin
Assert(False, 'JSON utility does not yet support published method getters for records');
end else begin
scriptObj := (dataPtr.GetSelf as TScriptObjInstance) as IScriptObj;
info := TInfoFunc.Create(progInfo, memberSymbol, progInfo.Execution.DataContext_Nil,
nil, scriptObj, TClassSymbol(compSym));
end;
StringifySymbol(exec, writer, memberSymbol.Typ, info.Call.GetDataPtr);
info := nil;
scriptObj := nil;
end;
var
i : Integer;
memberSymbolClass : TClass;
fieldSym : TFieldSymbol;
propSym : TPropertySymbol;
locData : IDataContext;
begin
if exec is TdwsProgramExecution then
progInfo := TdwsProgramExecution(exec).ProgramInfo
else progInfo := nil;
writer.BeginObject;
while compSym <> nil do begin
for i := 0 to compSym.Members.Count-1 do begin
memberSymbol := compSym.Members[i];
memberSymbolClass := memberSymbol.ClassType;
if memberSymbolClass = TPropertySymbol then begin
propSym := TPropertySymbol(memberSymbol);
if propSym.HasArrayIndices then
continue;
if (propSym.Visibility >= cvPublished) and (propSym.ReadSym <> nil) then begin
memberSymbol := propSym.ReadSym;
memberSymbolClass := memberSymbol.ClassType;
end else continue;
if propSym.HasExternalName then
writer.WriteName(propSym.ExternalName)
else writer.WriteName(propSym.Name);
end else if memberSymbolClass = TFieldSymbol then begin
if TFieldSymbol(memberSymbol).Visibility < cvPublished then
continue;
writer.WriteName(memberSymbol.Name);
end else continue;
if memberSymbolClass = TFieldSymbol then begin
fieldSym := TFieldSymbol(memberSymbol);
if fieldSym.Typ.IsBaseType then
StringifyVariant(exec, writer, dataPtr[fieldSym.Offset])
else begin
dataPtr.CreateOffset(fieldSym.Offset, locData);
StringifySymbol(exec, writer, fieldSym.Typ, locData);
end;
end else if memberSymbolClass.InheritsFrom(TFuncSymbol) then
DoGetter;
end;
compSym := compSym.Parent;
end;
writer.EndObject;
end;
// StringifyClass
//
class procedure JSONScript.StringifyClass(exec : TdwsExecution;
writer : TdwsJSONWriter; const obj : IScriptObj);
var
stringifyMeth : TMethodSymbol;
classSym : TClassSymbol;
progExec : TdwsProgramExecution;
methExpr : TFuncExprBase;
selfExpr : TTypedExpr;
buf : UnicodeString;
begin
if (obj=nil) or (obj.Destroyed) then begin
writer.WriteNull;
Exit;
end;
classSym := obj.ClassSym;
stringifyMeth := TMethodSymbol(classSym.Members.FindSymbol(SYS_JSON_STRINGIFY, cvPublic, TMethodSymbol));
if (stringifyMeth=nil)
or stringifyMeth.IsClassMethod
or (stringifyMeth.Params.Count <> 0)
or (not stringifyMeth.Typ.UnAliasedTypeIs(TBaseStringSymbol)) then begin
StringifyComposite(exec, writer, obj.ClassSym, obj);
end else begin
progExec := (exec as TdwsProgramExecution);
methExpr := nil;
selfExpr := TConstExpr.CreateValue(cNullPos, classSym, IUnknown(obj));
try
methExpr := CreateMethodExpr(
progExec.CompilerContext, stringifyMeth,
selfExpr, rkObjRef, cNullPos, []
);
methExpr.EvalAsUnicodeString(exec, buf);
writer.WriteJSON(buf);
finally
methExpr.Free;
selfExpr.Free;
end;
end;
end;
// StringifySetOf
//
class procedure JSONScript.StringifySetOf(exec : TdwsExecution; writer : TdwsJSONWriter; setOf : TSetOfSymbol;
const dataPtr : IDataContext);
var
offset, n : Integer;
value, mask, elem : UInt64;
elemSym : TElementSymbol;
begin
writer.BeginArray;
for offset := 0 to setOf.Size-1 do begin
value := UInt64(dataPtr.AsInteger[offset]);
if value = 0 then continue;
mask := 1;
for n := 0 to 63 do begin
if (mask and value) <> 0 then begin
elem := setOf.MinValue + (offset shl 6) + n;
elemSym := setOf.ElementByValue(elem);
if elemSym <> nil then
writer.WriteString(elemSym.StandardName)
else writer.WriteInteger(setOf.MinValue + (offset shl 6) + n);
value := value - mask;
if value = 0 then Break;
end;
mask := mask shl 1;
end;
end;
writer.EndArray;
end;
end.