-
Notifications
You must be signed in to change notification settings - Fork 61
/
buzztype.c
697 lines (641 loc) · 22.7 KB
/
buzztype.c
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include "buzzvm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/****************************************/
/****************************************/
#define BUZZTYPE_TABLE_BUCKETS 10
const char *buzztype_desc[] = { "nil", "integer", "float", "string", "table", "closure", "userdata" };
/****************************************/
/****************************************/
uint32_t buzzobj_table_hash(const void* key) {
buzzobj_t k = *(buzzobj_t*)key;
switch(k->o.type) {
case BUZZTYPE_INT: {
return (k->i.value % BUZZTYPE_TABLE_BUCKETS);
}
case BUZZTYPE_FLOAT: {
return ((uint32_t)(k->f.value) % BUZZTYPE_TABLE_BUCKETS);
}
case BUZZTYPE_STRING: {
return ((uint32_t)(k->s.value.sid) % BUZZTYPE_TABLE_BUCKETS);
}
default:
fprintf(stderr, "Can't use a %s value as table key\n", buzztype_desc[k->o.type]);
abort();
}
}
int buzzobj_table_keycmp(const void* a, const void* b) {
return buzzobj_cmp(*(buzzobj_t*)a, *(buzzobj_t*)b);
}
buzzobj_t buzzobj_new(uint16_t type) {
/* Create a new object. calloc() fills it with zeroes */
buzzobj_t o = (buzzobj_t)calloc(1, sizeof(union buzzobj_u));
/* Set the object type */
o->o.type = type;
/* Set the object marker */
o->o.marker = 0;
/* Take care of special initialization for specific types */
if(type == BUZZTYPE_TABLE) {
o->t.value = buzzdict_new(BUZZTYPE_TABLE_BUCKETS,
sizeof(buzzobj_t),
sizeof(buzzobj_t),
buzzobj_table_hash,
buzzobj_table_keycmp,
NULL);
}
else if(type == BUZZTYPE_CLOSURE) {
o->c.value.actrec = buzzdarray_new(1, sizeof(buzzobj_t), NULL);
}
/* All done */
return o;
}
/****************************************/
/****************************************/
void buzzobj_destroy(buzzobj_t* o) {
if((*o)->o.type == BUZZTYPE_TABLE) {
buzzdict_destroy(&((*o)->t.value));
}
else if((*o)->o.type == BUZZTYPE_CLOSURE) {
buzzdarray_destroy(&((*o)->c.value.actrec));
}
free(*o);
*o = NULL;
}
/****************************************/
/****************************************/
uint32_t buzzobj_hash(const buzzobj_t o) {
switch(o->o.type) {
case BUZZTYPE_NIL: {
return 0;
}
case BUZZTYPE_INT: {
return buzzdict_int32keyhash(&(o->i.value));
}
case BUZZTYPE_FLOAT: {
int32_t x = o->f.value;
return buzzdict_int32keyhash(&x);
}
case BUZZTYPE_STRING: {
return buzzdict_strkeyhash(&(o->s.value.str));
}
case BUZZTYPE_TABLE: {
uint32_t p = (uintptr_t)(o->t.value);
return buzzdict_uint32keyhash(&p);
}
case BUZZTYPE_USERDATA: {
uint32_t p = (uintptr_t)(o->u.value);
return buzzdict_uint32keyhash(&p);
}
case BUZZTYPE_CLOSURE:
default:
fprintf(stderr, "[BUG] %s:%d: Hash for Buzz object type %d\n", __FILE__, __LINE__, o->o.type);
abort();
}
}
/****************************************/
/****************************************/
int buzzobj_eq(const buzzobj_t a,
const buzzobj_t b) {
/* Make sure the type is the same */
if(a->o.type != b->o.type) return 0;
/* Equality means different things for different types */
switch(a->o.type) {
case BUZZTYPE_NIL: return 1;
case BUZZTYPE_INT: return (a->i.value == b->i.value);
case BUZZTYPE_FLOAT: return (a->f.value == b->f.value);
case BUZZTYPE_STRING: return 0; // TODO
case BUZZTYPE_TABLE: return ((uintptr_t)(a->t.value) == (uintptr_t)(b->t.value));
case BUZZTYPE_CLOSURE:
return((a->c.value.isnative == b->c.value.isnative) &&
(a->c.value.ref == b->c.value.ref) &&
(a->c.value.actrec == b->c.value.actrec));
case BUZZTYPE_USERDATA: return ((uintptr_t)(a->u.value) == (uintptr_t)(b->u.value));
default:
fprintf(stderr, "[BUG] %s:%d: Equality test between wrong Buzz objects types %d and %d\n", __FILE__, __LINE__, a->o.type, b->o.type);
abort();
}
}
/****************************************/
/****************************************/
static int buzzobj_strcmp(const char* s1, const char* s2) {
int c = strcmp(s1, s2);
if(c > 0) return 1;
if(c < 0) return -1;
return 0;
}
int buzzobj_cmp(const buzzobj_t a,
const buzzobj_t b) {
/* Nil */
if(a->o.type == BUZZTYPE_NIL && b->o.type == BUZZTYPE_NIL) {
return 0;
}
if(a->o.type == BUZZTYPE_NIL) {
return -1;
}
if(b->o.type == BUZZTYPE_NIL) {
return 1;
}
/* Numeric types */
if(a->o.type == BUZZTYPE_INT && b->o.type == BUZZTYPE_INT) {
if(a->i.value < b->i.value) return -1;
if(a->i.value > b->i.value) return 1;
return 0;
}
if(a->o.type == BUZZTYPE_INT && b->o.type == BUZZTYPE_FLOAT) {
if(a->i.value < b->f.value) return -1;
if(a->i.value > b->f.value) return 1;
return 0;
}
if(a->o.type == BUZZTYPE_FLOAT && b->o.type == BUZZTYPE_INT) {
if(a->f.value < b->i.value) return -1;
if(a->f.value > b->i.value) return 1;
return 0;
}
if(a->o.type == BUZZTYPE_FLOAT && b->o.type == BUZZTYPE_FLOAT) {
if(a->f.value < b->f.value) return -1;
if(a->f.value > b->f.value) return 1;
return 0;
}
/* String and other types */
if(a->o.type == BUZZTYPE_STRING && b->o.type == BUZZTYPE_STRING) {
return buzzobj_strcmp(a->s.value.str, b->s.value.str);
}
if(a->o.type == BUZZTYPE_STRING && b->o.type == BUZZTYPE_INT) {
char str[30];
sprintf(str, "%d", b->i.value);
return buzzobj_strcmp(a->s.value.str, str);
}
if(a->o.type == BUZZTYPE_STRING && b->o.type == BUZZTYPE_FLOAT) {
char str[30];
sprintf(str, "%f", b->f.value);
return buzzobj_strcmp(a->s.value.str, str);
}
if(a->o.type == BUZZTYPE_INT && b->o.type == BUZZTYPE_STRING) {
char str[30];
sprintf(str, "%d", a->i.value);
return buzzobj_strcmp(str, b->s.value.str);
}
if(a->o.type == BUZZTYPE_FLOAT && b->o.type == BUZZTYPE_STRING) {
char str[30];
sprintf(str, "%f", a->f.value);
return buzzobj_strcmp(str, b->s.value.str);
}
/* Tables */
if(a->o.type == BUZZTYPE_TABLE || b->o.type == BUZZTYPE_TABLE) {
/* Both operands are tables */
if(a->o.type == BUZZTYPE_TABLE && b->o.type == BUZZTYPE_TABLE) {
if((uintptr_t)(a->t.value) < (uintptr_t)(b->t.value)) return -1;
if((uintptr_t)(a->t.value) > (uintptr_t)(b->t.value)) return 1;
return 0;
}
/* Anything is smaller than a table */
if(a->o.type == BUZZTYPE_TABLE) {
return 1;
}
if(b->o.type == BUZZTYPE_TABLE) {
return -1;
}
}
/* Userdata */
if(a->o.type == BUZZTYPE_USERDATA && b->o.type == BUZZTYPE_USERDATA) {
if((uintptr_t)(a->u.value) < (uintptr_t)(b->u.value)) return -1;
if((uintptr_t)(a->u.value) > (uintptr_t)(b->u.value)) return 1;
return 0;
}
// TODO better error management
fprintf(stderr, "[TODO] %s:%d: Error for comparison between Buzz objects of types %d and %d\n", __FILE__, __LINE__, a->o.type, b->o.type);
abort();
}
/****************************************/
/****************************************/
int buzzobj_type(buzzvm_t vm) {
/* Get parameter */
buzzvm_lnum_assert(vm, 1);
buzzvm_lload(vm, 1);
buzzobj_t o = buzzvm_stack_at(vm, 1);
buzzvm_pop(vm);
/* Return a string with the type */
buzzvm_pushs(vm, buzzvm_string_register(vm, buzztype_desc[o->o.type], 0));
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
int buzzobj_int(struct buzzvm_s* vm) {
/* Get float parameter and make sure it's a float */
buzzvm_lnum_assert(vm, 1);
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_FLOAT);
buzzobj_t f = buzzvm_stack_at(vm, 1);
buzzvm_pop(vm);
buzzvm_pushi(vm, f->f.value);
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
int buzzobj_float(struct buzzvm_s* vm) {
/* Get int parameter and make sure it's an int */
buzzvm_lnum_assert(vm, 1);
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_INT);
buzzobj_t i = buzzvm_stack_at(vm, 1);
buzzvm_pop(vm);
buzzvm_pushf(vm, i->i.value);
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
int buzzobj_clone(buzzvm_t vm) {
/* Get parameter */
buzzvm_lnum_assert(vm, 1);
buzzvm_lload(vm, 1);
buzzobj_t o = buzzvm_stack_at(vm, 1);
buzzvm_pop(vm);
/* Return a clone of the object */
buzzvm_push(vm, buzzheap_clone(vm, o));
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
int buzzobj_size(buzzvm_t vm) {
/* Get table parameter and make sure it's a table */
buzzvm_lnum_assert(vm, 1);
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE);
buzzobj_t t = buzzvm_stack_at(vm, 1);
buzzvm_pop(vm);
buzzvm_pushi(vm, buzzdict_size(t->t.value));
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
struct buzzobj_foreach_params {
buzzvm_t vm;
buzzobj_t fun;
};
void buzzobj_foreach_entry(const void* key, void* data, void* params) {
/* Cast params */
struct buzzobj_foreach_params* p = (struct buzzobj_foreach_params*)params;
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Push closure and params (key and value) */
buzzvm_push(p->vm, p->fun);
buzzvm_push(p->vm, *(buzzobj_t*)key);
buzzvm_push(p->vm, *(buzzobj_t*)data);
/* Call closure */
p->vm->state = buzzvm_closure_call(p->vm, 2);
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Get rid of return value */
buzzvm_pop(p->vm);
}
int buzzobj_foreach(buzzvm_t vm) {
/* Table and closure expected */
buzzvm_lnum_assert(vm, 2);
/* Get table */
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE);
buzzobj_t t = buzzvm_stack_at(vm, 1);
/* Get closure */
buzzvm_lload(vm, 2);
buzzvm_type_assert(vm, 1, BUZZTYPE_CLOSURE);
buzzobj_t c = buzzvm_stack_at(vm, 1);
/* Go through the table element and apply the closure */
struct buzzobj_foreach_params p = { .vm = vm, .fun = c };
buzzdict_foreach(t->t.value, buzzobj_foreach_entry, &p);
return buzzvm_ret0(vm);
}
/****************************************/
/****************************************/
struct buzzobj_map_params {
buzzvm_t vm;
buzzobj_t fun;
buzzdict_t result;
};
void buzzobj_map_entry(const void* key, void* data, void* params) {
/* Cast params */
struct buzzobj_map_params* p = (struct buzzobj_map_params*)params;
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Save current stack size */
uint32_t ss = buzzvm_stack_top(p->vm);
/* Push closure and params (key and value) */
buzzvm_push(p->vm, p->fun);
buzzvm_push(p->vm, *(buzzobj_t*)key);
buzzvm_push(p->vm, *(buzzobj_t*)data);
/* Call closure */
p->vm->state = buzzvm_closure_call(p->vm, 2);
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Make sure a value was returned */
if(buzzvm_stack_top(p->vm) <= ss) {
/* Error */
buzzvm_seterror(p->vm,
BUZZVM_ERROR_STACK,
"map(table,function) expects the function to return a value");
return;
}
/* Manage return value */
buzzobj_t r = buzzvm_stack_at(p->vm, 1);
if(r->o.type != BUZZTYPE_NIL) {
buzzdict_set(p->result, key, &r);
}
else {
buzzdict_remove(p->result, key);
}
/* Get rid of return value */
buzzvm_pop(p->vm);
}
int buzzobj_map(buzzvm_t vm) {
/* Table and closure expected */
buzzvm_lnum_assert(vm, 2);
/* Get table */
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE);
buzzobj_t t = buzzvm_stack_at(vm, 1);
/* Get closure */
buzzvm_lload(vm, 2);
buzzvm_type_assert(vm, 1, BUZZTYPE_CLOSURE);
buzzobj_t c = buzzvm_stack_at(vm, 1);
/* Create a table as the return value */
buzzobj_t r = buzzheap_newobj(vm, BUZZTYPE_TABLE);
buzzvm_push(vm, r);
/* Go through the table element and apply the closure */
struct buzzobj_map_params p = {
.vm = vm,
.fun = c,
.result = r->t.value
};
buzzdict_foreach(t->t.value, buzzobj_map_entry, &p);
/* Return the table */
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
struct buzzobj_reduce_params {
buzzvm_t vm;
buzzobj_t fun;
};
void buzzobj_reduce_entry(const void* key, void* data, void* params) {
/* Cast params */
struct buzzobj_reduce_params* p = (struct buzzobj_reduce_params*)params;
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Save and pop accumulator from the stack */
buzzobj_t accum = buzzvm_stack_at(p->vm, 1);
buzzvm_pop(p->vm);
/* Save current stack size */
uint32_t ss = buzzvm_stack_top(p->vm);
/* Push closure and params (key and value) */
buzzvm_push(p->vm, p->fun);
buzzvm_push(p->vm, *(buzzobj_t*)key);
buzzvm_push(p->vm, *(buzzobj_t*)data);
buzzvm_push(p->vm, accum);
/* Call closure */
p->vm->state = buzzvm_closure_call(p->vm, 3);
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Make sure a value was returned */
if(buzzvm_stack_top(p->vm) <= ss)
/* Error */
buzzvm_seterror(p->vm,
BUZZVM_ERROR_STACK,
"reduce(table,function,accumulator) expects the function to return a value");
}
int buzzobj_reduce(buzzvm_t vm) {
/* Table, closure, and initial value expected */
buzzvm_lnum_assert(vm, 3);
/* Get table */
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE);
buzzobj_t t = buzzvm_stack_at(vm, 1);
/* Get closure */
buzzvm_lload(vm, 2);
buzzvm_type_assert(vm, 1, BUZZTYPE_CLOSURE);
buzzobj_t c = buzzvm_stack_at(vm, 1);
/* Put initial accumulator value on the stack */
buzzvm_lload(vm, 3);
/* Go through the table element and apply the closure */
struct buzzobj_reduce_params p = { .vm = vm, .fun = c };
buzzdict_foreach(t->t.value, buzzobj_reduce_entry, &p);
/* The final value of the accumulator is on the stack */
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
struct buzzobj_filter_params {
buzzvm_t vm;
buzzobj_t fun;
buzzdict_t result;
};
void buzzobj_filter_entry(const void* key, void* data, void* params) {
/* Cast params */
struct buzzobj_filter_params* p = (struct buzzobj_filter_params*)params;
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Save current stack size */
uint32_t ss = buzzvm_stack_top(p->vm);
/* Push closure and params (key and value) */
buzzvm_push(p->vm, p->fun);
buzzvm_push(p->vm, *(buzzobj_t*)key);
buzzvm_push(p->vm, *(buzzobj_t*)data);
/* Call closure */
p->vm->state = buzzvm_closure_call(p->vm, 2);
if(p->vm->state != BUZZVM_STATE_READY) return;
/* Make sure a value was returned */
if(buzzvm_stack_top(p->vm) <= ss) {
/* Error */
buzzvm_seterror(p->vm,
BUZZVM_ERROR_STACK,
"filter(table,predicate) expects the predicate to return a value");
return;
}
/* Add entry to the return table */
buzzobj_t retval = buzzvm_stack_at(p->vm, 1);
if(retval->o.type != BUZZTYPE_NIL &&
(retval->o.type != BUZZTYPE_INT ||
retval->i.value != 0)) {
buzzdict_set(p->result, key, data);
}
/* Get rid of return value */
buzzvm_pop(p->vm);
}
int buzzobj_filter(buzzvm_t vm) {
/* Table and closure expected */
buzzvm_lnum_assert(vm, 2);
/* Get table */
buzzvm_lload(vm, 1);
buzzvm_type_assert(vm, 1, BUZZTYPE_TABLE);
buzzobj_t t = buzzvm_stack_at(vm, 1);
/* Get closure */
buzzvm_lload(vm, 2);
buzzvm_type_assert(vm, 1, BUZZTYPE_CLOSURE);
buzzobj_t c = buzzvm_stack_at(vm, 1);
/* Create a table as the return value */
buzzobj_t r = buzzheap_newobj(vm, BUZZTYPE_TABLE);
buzzvm_push(vm, r);
/* Go through the table element and apply the closure */
struct buzzobj_filter_params p = {
.vm = vm,
.fun = c,
.result = r->t.value
};
buzzdict_foreach(t->t.value, buzzobj_filter_entry, &p);
/* Return the table */
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
void buzzobj_serialize_tableelem(const void* key, void* data, void* params) {
buzzobj_serialize((buzzdarray_t)params, *(buzzobj_t*)key);
buzzobj_serialize((buzzdarray_t)params, *(buzzobj_t*)data);
}
void buzzobj_serialize(buzzdarray_t buf,
const buzzobj_t data) {
buzzmsg_serialize_u8(buf, data->o.type);
switch(data->o.type) {
case BUZZTYPE_NIL: {
break;
}
case BUZZTYPE_INT: {
buzzmsg_serialize_u32(buf, data->i.value);
break;
}
case BUZZTYPE_FLOAT: {
buzzmsg_serialize_float(buf, data->f.value);
break;
}
case BUZZTYPE_STRING: {
buzzmsg_serialize_string(buf, data->s.value.str);
break;
}
case BUZZTYPE_TABLE: {
buzzmsg_serialize_u8(buf, buzzdict_size(data->t.value));
buzzdict_foreach(data->t.value, buzzobj_serialize_tableelem, buf);
break;
}
case BUZZTYPE_CLOSURE: {
// TODO here we assume that the first and only element of the
// activation record is nil, which is true only for basic
// functions. For table closures, we currently have no check,
// so while table closures technically can pass the
// subsequent test, they cannot in fact be serialized
// correctly. More work is necessary to serialize the
// closures completely. The only supported cases are
// serializing a function and the test case in
// testmobilecode.bzz, which involves a table.
if(buzzdarray_size(data->c.value.actrec) == 1) {
buzzmsg_serialize_u8(buf, data->c.value.isnative);
buzzmsg_serialize_u32(buf, data->c.value.ref);
}
else {
fprintf(stderr, "[TODO] %s:%d: can't serialize a nested closure\n", __FILE__, __LINE__);
}
break;
}
default:
fprintf(stderr, "[TODO] %s:%d Can't serialize an object of type %s\n", __FILE__, __LINE__, buzztype_desc[data->o.type]);
}
}
/****************************************/
/****************************************/
int64_t buzzobj_deserialize(buzzobj_t* data,
buzzdarray_t buf,
uint32_t pos,
struct buzzvm_s* vm) {
int64_t p = pos;
uint8_t type;
p = buzzmsg_deserialize_u8(&type, buf, p);
if(p < 0) return -1;
*data = buzzheap_newobj(vm, type);
switch(type) {
case BUZZTYPE_NIL: {
return p;
}
case BUZZTYPE_INT: {
return buzzmsg_deserialize_u32((uint32_t*)(&((*data)->i.value)), buf, p);
}
case BUZZTYPE_FLOAT: {
return buzzmsg_deserialize_float(&((*data)->f.value), buf, p);
}
case BUZZTYPE_STRING: {
char* str;
p = buzzmsg_deserialize_string(&str, buf, p);
if(p < 0) return -1;
(*data)->s.value.sid = buzzstrman_register(vm->strings, str, 0);
(*data)->s.value.str = buzzstrman_get(vm->strings, (*data)->s.value.sid);
free(str);
return p;
}
case BUZZTYPE_TABLE: {
uint8_t size;
uint16_t i;
p = buzzmsg_deserialize_u8(&size, buf, p);
if(p < 0) return -1;
for(i = 0; i < size; ++i) {
buzzobj_t k;
buzzobj_t v;
p = buzzobj_deserialize(&k, buf, p, vm);
if(p < 0) return -1;
p = buzzobj_deserialize(&v, buf, p, vm);
if(p < 0) return -1;
buzzdict_set((*data)->t.value, &k, &v);
}
return p;
}
case BUZZTYPE_CLOSURE: {
buzzobj_t nil = buzzheap_newobj(vm, BUZZTYPE_NIL);
buzzdarray_push((*data)->c.value.actrec, &nil);
p = buzzmsg_deserialize_u8(&((*data)->c.value.isnative), buf, p);
if(p < 0) return -1;
return buzzmsg_deserialize_u32((uint32_t*)(&((*data)->c.value.ref)), buf, p);
}
default:
fprintf(stderr, "[TODO] %s:%d Can't deserialize an object of type %s\n", __FILE__, __LINE__, buzztype_desc[type]);
return -1;
}
}
/****************************************/
/****************************************/
#define make_buzzobj_closure_is(TYPE) \
int buzzobj_closure_is ## TYPE(buzzvm_t vm) { \
/* Make sure there's a parameter */ \
buzzvm_lnum_assert(vm, 1); \
/* Get parameter */ \
buzzvm_lload(vm, 1); \
/* Push return value */ \
buzzvm_pushi(vm, buzzobj_is ## TYPE(buzzvm_stack_at(vm, 1))); \
/* Return it */ \
return buzzvm_ret1(vm); \
}
make_buzzobj_closure_is(nil);
make_buzzobj_closure_is(int);
make_buzzobj_closure_is(float);
make_buzzobj_closure_is(string);
make_buzzobj_closure_is(table);
make_buzzobj_closure_is(closure);
make_buzzobj_closure_is(userdata);
/****************************************/
/****************************************/
#define function_register_base(FNAME) \
buzzvm_pushs(vm, buzzvm_string_register(vm, #FNAME, 1)); \
buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzobj_ ## FNAME)); \
buzzvm_gstore(vm);
#define function_register_istype(FNAME) \
buzzvm_pushs(vm, buzzvm_string_register(vm, "is" #FNAME, 1)); \
buzzvm_pushcc(vm, buzzvm_function_register(vm, buzzobj_closure_is ## FNAME)); \
buzzvm_gstore(vm);
int buzzobj_register(struct buzzvm_s* vm) {
function_register_base(type);
function_register_base(int);
function_register_base(float);
function_register_base(clone);
function_register_base(size);
function_register_base(foreach);
function_register_base(map);
function_register_base(reduce);
function_register_base(filter);
function_register_istype(nil);
function_register_istype(int);
function_register_istype(float);
function_register_istype(string);
function_register_istype(table);
function_register_istype(closure);
function_register_istype(userdata);
return vm->state;
}
/****************************************/
/****************************************/