summaryrefslogtreecommitdiff
path: root/packages/libsee/examples/mod_stream.pp
blob: 35e12a1d16572fc06821ea6689fd355b556da448 (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
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
unit mod_stream;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, libsee;

Procedure RegisterStreamModule;
Procedure RegisterWriteModule;

implementation

{ ---------------------------------------------------------------------
  General auxiliary functions
  ---------------------------------------------------------------------}

Function ValueToString(V : TSee_Value) : string;

Var
  PS : Ptcuint;
  PD : PChar;
  I : Integer;

begin
  SetLength(Result,v.u._string^.length);
  If Length(Result)<>0 then
    begin
    PD:=PChar(Result);
    PS:=v.u._string^.data;
    For I:=0 to length(Result)-1 do
      begin
      PD^:=Char(PS^ and $ff);
      Inc(PD);
      Inc(PS);
      end;
    end;
end;

Procedure CreateJSObject(Interp : PSEE_Interpreter; Parent : PSEE_Object;AName : PSEE_String; Obj : PSee_Object);

var
  V : PSEE_Value;

begin
  v:=new_see_value;
  see_set_object(V,Obj);
  see_object_put(interp,parent,AName,V,SEE_ATTR_DEFAULT);
end;

Procedure CreateJSNumber(Interp : PSEE_Interpreter; Obj : PSee_Object; AName : PSEE_String; AValue : TSEE_number_t);

var
  V : PSEE_Value;

begin
  v:=new_SEE_value;
  see_set_number(V,AValue);
  see_object_put(Interp,Obj,AName,v,SEE_ATTR_DEFAULT);
end;

Procedure CreateJSFunction(Interp : PSEE_Interpreter; Obj : PSee_Object; Func : TSEE_call_fn_t; AName : PSEE_String; Len : Integer);

var
  V : PSEE_Value;

begin
  v:=new_SEE_value;
  see_set_object(V,see_cfunction_make(interp,Func,AName,len));
  see_object_put(Interp,Obj,AName,v,SEE_ATTR_DEFAULT);
end;

{ ---------------------------------------------------------------------
  Stream module support
  ---------------------------------------------------------------------}
Var
  StreamModule : TSEE_module;
  StreamObjectDef,
  StreamPrototypeDef : PSEE_objectclass;

  WriteModule : TSEE_module;

Type
  TStreamModuleData = record
    Stream : PSEE_object;
    Prototype :  PSEE_object;
    Error : PSEE_object;
  end;
  PStreamModuleData = ^TStreamModuleData;

  TStreamObject = record
    native : TSEE_native;
    Stream : TStream;
  end;
  PSTreamObject = ^TStreamObject;

Var
  GStreamRead,
  GStreamWrite,
  GStreamSeek,
  GStreamSize,
  GStreamPosition,
  GStreamFree,
  GStreamfmCreate,
  GStreamfmOpenRead,
  GStreamfmOpenWrite,
  GStreamfmOpenReadWrite,
  GStreamStream,
  GStreamError,
  GStreamPrototype : PSEE_String;

Procedure StreamAlloc(Interp : PSEE_Interpreter); cdecl;

begin
  PPointer(see_module_private(Interp,@StreamModule))^:=new(PStreamModuleData);
end;

Function PrivateData(Interp : PSEE_Interpreter) : PStreamModuleData;
begin
  Result:=PStreamModuleData((see_module_private(Interp,@StreamModule))^)
end;

Function AsFile(i:PTSEE_interpreter; obj:PTSEE_object) : PStreamObject;

begin
  If (Not Assigned(obj)) or (Obj^.objectclass<>StreamPrototypeDef) then
      SEE_error__throw0(i,I^.TypeError,Nil);
  Result:=PStreamObject(Obj)
end;

procedure StreamSize (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  S : PStreamObject;

begin
  S:=AsFile(I,ThisObj);
  If (S^.Stream=Nil) then
    SEE_error__throw0(i,PrivateData(I)^.Error,Nil);
  SEE_SET_NUMBER(res,S^.Stream.Size);
end;


procedure StreamWrite (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  S : PStreamObject;
  v : TSEE_Value;
  t : string;

begin
  S:=AsFile(I,ThisObj);
  If (S^.Stream=Nil) then
    SEE_error__throw0(i,PrivateData(I)^.Error,'File is closed');
  if (ArgC=0) then
    SEE_error__throw0(i,I^.RangeError,'Missing argument');
  SEE_ToString(i,argv[0], @v);
  T:=ValueToString(V);
  If Length(T)>0 then
    S^.Stream.Write(T[1],Length(T));
end;

procedure StreamPosition (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  S : PStreamObject;
  v : TSEE_Value;
  t : string;

begin
  S:=AsFile(I,ThisObj);
  If (S^.Stream=Nil) then
    SEE_error__throw0(i,PrivateData(I)^.Error,'File is closed');
  SEE_SET_NUMBER(res,S^.Stream.Position);
end;

procedure StreamSeek (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  S : PStreamObject;
  v : TSEE_Value;
  newpos : integer;

begin
  S:=AsFile(I,ThisObj);
  If (S^.Stream=Nil) then
    SEE_error__throw0(i,PrivateData(I)^.Error,'File is closed');
  if (ArgC=0) then
    SEE_error__throw0(i,I^.RangeError,'Missing argument');
  newpos:=SEE_ToUint32(i,argv[0]);
  SEE_SET_NUMBER(res,S^.Stream.Seek(soFromBeginning,newpos));
end;

procedure StreamRead (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  S : PStreamObject;
  r : PSEE_String;
  j,maxlen : integer;
  c : char;

begin
  S:=AsFile(I,ThisObj);
  If (S^.Stream=Nil) then
    SEE_error__throw0(i,PrivateData(I)^.Error,'File is closed');
  if (ArgC=0) then
    maxlen:=1024
  else
    maxlen:=see_touint32(I,argv[0]);
  r:=see_string_new(I,maxlen);
  For j:=0 to maxLen-1 do
    begin
    S^.stream.Read(c,sizeOf(c));
    SEE_string_addch(R,ord(c));
    end;
  SEE_SET_STRING(Res,r);
end;

procedure StreamFree (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  S : PStreamObject;
  v : TSEE_Value;
  t : string;

begin
  S:=AsFile(I,ThisObj);
  If (S^.Stream=Nil) then
    SEE_error__throw0(i,PrivateData(I)^.Error,'File is closed');
  FreeAndNil(S^.Stream);
  SEE_SET_UNDEFINED(Res);
end;

procedure StreamFinalize ( i:PTSEE_interpreter; p:pointer; closure:pointer);cdecl;

begin
  FreeAndNil(PStreamObject(P)^.Stream);
end;

procedure StreamConstruct (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  P : PChar;
  fm : Integer;
  S : TStream;
  Err : String;
  R : PTSEE_Object;

begin
  SEE_parse_args(i,argc,argv,'Z|i',@p,@fm);
  If (P=Nil) then
    SEE_error__throw0(i,I^.RangeError,'Missing argument');
  Err:='';
  try
    S:=TFileStream.Create(strpas(p),fm);
  except
    On E : Exception do
      Err:=E.Message;
  end;
  If (Err<>'') then
    SEE_error__throw0(i,PrivateData(I)^.Error,PChar(Err));
  R:=PTSEE_Object(SEE_malloc_finalize(I,SizeOf(TStreamObject),@StreamFinalize,Nil));
  SEE_Native_init(PSEE_Native(R),I,StreamPrototypeDef,PrivateData(I)^.Prototype);
  PStreamObject(r)^.Stream:=S;
  SEE_SET_OBJECT(Res,R);
end;



Procedure StreamInit(Interp : PSEE_Interpreter); cdecl;

Var
  Stream,
  StreamPrototype,
  StreamError : PSee_object;
begin
//  writeln('Initializing stream');
  // Construct Stream.prototype object
//  writeln('Creating Stream Prototype ');
  StreamPrototype:=PSEE_object(SEE_malloc(Interp,SizeOf(TSTreamObject)));
  See_native_init(PSEE_native(StreamProtoType),Interp,StreamPrototypeDef,interp^.Object_prototype);
  PSTreamObject(StreamPrototype)^.stream:=Nil;
  createJSFUnction(Interp,StreamPrototype,@StreamRead,GStreamRead,0);
  createJSFUnction(Interp,StreamPrototype,@StreamWrite,GStreamWrite,0);
  createJSFUnction(Interp,StreamPrototype,@StreamSize,GStreamSize,0);
  createJSFUnction(Interp,StreamPrototype,@StreamPosition,GStreamPosition,0);
  createJSFUnction(Interp,StreamPrototype,@StreamSeek,GStreamSeek,0);
  createJSFUnction(Interp,StreamPrototype,@StreamFree,GStreamFree,0);
//  writeln('Creating Stream');
  // Construct Stream object
  Stream:=PSEE_object(new_see_native);
  See_native_init(PSEE_native(Stream),Interp,StreamObjectDef,interp^.Object_prototype);
  CreateJSObject(Interp,Interp^.Global,GStreamStream,Stream);
  CreateJSObject(Interp,Stream,GStreamprototype,StreamPrototype);
  CreateJSNumber(Interp,Stream,GStreamfmCreate,fmCreate);
  CreateJSNumber(Interp,Stream,GStreamfmOpenRead,fmOpenRead);
  CreateJSNumber(Interp,Stream,GStreamfmOpenWrite,fmOpenWrite);
  CreateJSNumber(Interp,Stream,GStreamfmOpenReadWrite,fmOpenReadWrite);
  StreamError:=SEE_Error_make(interp, GSTreamError);
  PrivateData(Interp)^.Stream:=STream;
  PrivateData(Interp)^.Prototype:=StreamPrototype;
  PrivateData(Interp)^.Error:=StreamError;
//  writeln('Done initializing stream');
end;

Procedure AllocateStreamStrings;

begin
  GStreamRead:=SEE_intern_global('Read');
  GStreamWrite:=SEE_intern_global('Write');
  GStreamSeek:=SEE_intern_global('Seek');
  GStreamSize:=SEE_intern_global('Size');
  GStreamPosition:=SEE_intern_global('Position');
  GStreamFree:=SEE_intern_global('Free');
  GStreamfmCreate:=SEE_intern_global('fmCreate');
  GStreamfmOpenRead:=SEE_intern_global('fmOpenRead');
  GStreamfmOpenWrite:=SEE_intern_global('fmOpenWrite');
  GStreamfmOpenReadWrite:=SEE_intern_global('fmOpenReadWrite');
  GStreamStream:=SEE_intern_global('Stream');
  GStreamError:=SEE_intern_global('Error');
  GStreamPrototype:=SEE_intern_global('prototype');
end;

Function StreamInitModule : Integer; cdecl;

begin
//  writeln('Initializing module');
  StreamPrototypeDef:=new_SEE_objectclass;
  With StreamPrototypeDef^ do
    begin
    _Class:='Stream';
    get:=SEE_native_get;
    put:=SEE_native_put;
    canput:=SEE_native_canput;
    hasproperty:=SEE_native_hasproperty;
    Delete:=SEE_native_delete;
    DefaultValue:=SEE_native_defaultvalue;
    ENumerator:=SEE_native_enumerator;
    Construct:=Nil;
    Call:=Nil;
    HasInstance:=Nil;
    end;
  StreamObjectDef:=new_SEE_objectclass;
  With StreamObjectDef^ do
    begin
    _Class:='Stream';
    get:=SEE_native_get;
    put:=SEE_native_put;
    get:=SEE_native_get;
    put:=SEE_native_put;
    canput:=SEE_native_canput;
    hasproperty:=SEE_native_hasproperty;
    Delete:=SEE_native_delete;
    DefaultValue:=SEE_native_defaultvalue;
    ENumerator:=SEE_native_enumerator;
    Construct:=@StreamConstruct;
    Call:=Nil;
    HasInstance:=Nil;
    end;
  AllocateStreamStrings;
//  writeln('Done Initializing module');
  Result:=0;
end;

Procedure RegisterStreamModule;

begin
//  writeln('Registering stream module');
//  StreamModule:=new_SEE_module;
  With StreamModule do
    begin
    magic:=SEE_MODULE_MAGIC;
    name:='Stream';
    version:='1.0';
    Index:=0;
    Mod_init:=@StreamInitModule;
    alloc:=@StreamAlloc;
    init:=@StreamInit
    end;
  SEE_module_add(@StreamModule);
end;

{ ---------------------------------------------------------------------
  Write(ln) module support
  ---------------------------------------------------------------------}

procedure WriteWrite (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;

Var
  a,C : Integer;
  t : string;
  v : TSEE_Value;

begin
  if (ArgC=0) then
    SEE_error__throw0(i,I^.RangeError,'Missing argument');
  C:=0;
  For A:=0 to Argc-1 do
    begin
    SEE_ToString(i,argv[a], @v);
    T:=ValueToString(V);
    If Length(T)>0 then
      begin
      Write(T);
      C:=C+Length(T);
      end;
    end;
  SEE_SET_NUMBER(Res,C);
end;

procedure WriteWriteln (i:PTSEE_interpreter; obj:PTSEE_object; thisobj:PTSEE_object; argc:Tcint; argv:PPTSEE_value;
             res:PTSEE_value);cdecl;


begin
  if (Argc>0) then
    WriteWrite(i,obj,thisobj,argc,argv,res)
  else
    SEE_SET_NUMBER(Res,0);
  Writeln;
end;

Var
  GWriteWrite   : PSEE_STRING;
  GWriteWriteln : PSEE_STRING;

Procedure WriteInit(Interp : PSEE_Interpreter); cdecl;

begin
//  writeln('Initializing write');
  createJSFUnction(Interp,Interp^.Global,@WriteWrite,GWriteWrite,1);
  createJSFUnction(Interp,Interp^.Global,@WriteWriteln,GWriteWriteln,1);
//  writeln('Done initializing write');
end;

Procedure AllocateWriteStrings;

begin
  GWriteWrite:=SEE_intern_global('write');
  GWriteWriteln:=SEE_intern_global('writeln');
end;

Function WriteInitModule : Integer; cdecl;
begin
  Result:=0;
end;

Procedure RegisterWriteModule;

begin
//  writeln('Registering write module');
//  StreamModule:=new_SEE_module;
  With WriteModule do
    begin
    magic:=SEE_MODULE_MAGIC;
    name:='Write';
    version:='1.0';
    Index:=0;
    Mod_init:=@WriteInitModule;
    alloc:=Nil;
    init:=@WriteInit
    end;
  AllocateWriteStrings;
  SEE_module_add(@WriteModule);
end;


end.