/
nqp.ops
802 lines (600 loc) · 21.9 KB
/
nqp.ops
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
BEGIN_OPS_PREAMBLE
/* Parroty includes. */
#include "parrot/parrot.h"
#include "parrot/extend.h"
#include "parrot/dynext.h"
/* 6modely includes. */
#include "../metamodel/sixmodelobject.h"
#include "../metamodel/repr_registry.h"
#include "../pmc/pmc_dispatchersub.h"
#include "../pmc/pmc_nqpmultisig.h"
#include "../metamodel/multi_dispatch.h"
#include "../metamodel/serialization_context.h"
#include "pmc_sub.h"
/* Did we do the dynop setup yet?
* XXX Relies on this happening once in a single thread at startup. */
static INTVAL initialized = 0;
/* Cached type IDs. */
static INTVAL stable_id = 0;
static INTVAL repr_id = 0;
static INTVAL smo_id = 0;
static INTVAL disp_id = 0;
static INTVAL ms_id = 0;
/* Built-in HOWs (meta-objects). */
static PMC *KnowHOW = NULL;
END_OPS_PREAMBLE
/*
=item nqp_dynop_setup()
Does various setup tasks for the benefit of the other dynops.
=cut
*/
inline op nqp_dynop_setup() :base_core {
if (!initialized) {
initialized = 1;
/* Look up and cache some type IDs. */
stable_id = pmc_type(interp, Parrot_str_new(interp, "STable", 0));
repr_id = pmc_type(interp, Parrot_str_new(interp, "REPR", 0));
smo_id = pmc_type(interp, Parrot_str_new(interp, "SixModelObject", 0));
disp_id = pmc_type(interp, Parrot_str_new(interp, "DispatcherSub", 0));
ms_id = pmc_type(interp, Parrot_str_new(interp, "NQPMultiSig", 0));
/* Initialize the object model. */
KnowHOW = SixModelObject_initialize(interp);
/* XXX Kill this once everything uses get_knowhow. */
Parrot_ns_set_global(interp, Parrot_hll_get_ctx_HLL_namespace(interp),
Parrot_str_new(interp, "KnowHOW", 0), KnowHOW);
}
}
/*
=item get_knowhow()
Returns the 6model core meta-object, KnowHOW.
=cut
*/
inline op get_knowhow(out PMC) :base_core {
if (KnowHOW)
$1 = KnowHOW;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"6model not yet initialized; cannot use get_knowhow");
}
/*
=item get_how(obj)
Gets the HOW for a 6model Object.
=cut
*/
inline op get_how(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = STABLE($2)->HOW;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use get_how on a SixModelObject");
}
/*
=item get_what(obj)
Gets the WHAT for a 6model Object.
=cut
*/
inline op get_what(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = STABLE($2)->WHAT;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use get_what on a SixModelObject");
}
/*
=item get_who(obj)
Gets the WHO for a 6model Object.
=cut
*/
inline op get_who(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = STABLE($2)->WHO;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use get_who on a SixModelObject");
}
/*
=item set_who(obj)
Sets the WHO for a 6model Object.
=cut
*/
inline op set_who(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == smo_id)
STABLE($1)->WHO = $2;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use set_who on a SixModelObject");
}
/*
=item repr_type_object_for(how, repr_name)
Creates a type object associated with the given HOW and of the given
representation.
=cut
*/
inline op repr_type_object_for(out PMC, in PMC, in STR) :base_core {
PMC *REPR = REPR_get_by_name(interp, $3);
$1 = REPR_STRUCT(REPR)->type_object_for(interp, REPR, $2);
}
/*
=item repr_instance_of()
Instantiates a new object based on the given WHAT.
=cut
*/
inline op repr_instance_of(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->instance_of(interp, REPR_PMC($2), $2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_instance_of on a SixModelObject");
}
/*
=item repr_defined()
Checks the REPRs idea of definedness.
=cut
*/
inline op repr_defined(out INT, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->defined(interp, REPR_PMC($2), $2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_defined on a SixModelObject");
}
/*
=item repr_get_attr_int()
Gets the specified int attribute. Note that the attribute must be stored as
a native int - the repr is not obligated to do boxing/unboxing for you.
=cut
*/
inline op repr_get_attr_int(out INT, in PMC, in PMC, in STR) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->get_attribute_int(interp, REPR_PMC($2), $2, $3, $4, NO_HINT);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_get_attr_int on a SixModelObject");
}
/*
=item repr_get_attr_num()
Gets the specified num attribute. Note that the attribute must be stored as
a native num - the repr is not obligated to do boxing/unboxing for you.
=cut
*/
inline op repr_get_attr_int(out NUM, in PMC, in PMC, in STR) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->get_attribute_num(interp, REPR_PMC($2), $2, $3, $4, NO_HINT);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_get_attr_num on a SixModelObject");
}
/*
=item repr_get_attr_str()
Gets the specified str attribute. Note that the attribute must be stored as
a native str - the repr is not obligated to do boxing/unboxing for you.
=cut
*/
inline op repr_get_attr_str(out STR, in PMC, in PMC, in STR) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->get_attribute_str(interp, REPR_PMC($2), $2, $3, $4, NO_HINT);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_get_attr_str on a SixModelObject");
}
/*
=item repr_bind_attr_int()
Binds the specified int attribute. Note that the attribute must be stored as
a native int - the repr is not obligated to do boxing/unboxing for you.
=cut
*/
inline op repr_bind_attr_int(in PMC, in PMC, in STR, in INT) :base_core {
if ($1->vtable->base_type == smo_id)
REPR($1)->bind_attribute_int(interp, REPR_PMC($1), $1, $2, $3, NO_HINT, $4);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_bind_attr_int on a SixModelObject");
}
/*
=item repr_bind_attr_num()
Binds the specified num attribute. Note that the attribute must be stored as
a native num - the repr is not obligated to do boxing/unboxing for you.
=cut
*/
inline op repr_bind_attr_num(in PMC, in PMC, in STR, in NUM) :base_core {
if ($1->vtable->base_type == smo_id)
REPR($1)->bind_attribute_num(interp, REPR_PMC($1), $1, $2, $3, NO_HINT, $4);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_bind_attr_num on a SixModelObject");
}
/*
=item repr_bind_attr_str()
Binds the specified str attribute. Note that the attribute must be stored as
a native str - the repr is not obligated to do boxing/unboxing for you.
=cut
*/
inline op repr_bind_attr_num(in PMC, in PMC, in STR, in STR) :base_core {
if ($1->vtable->base_type == smo_id)
REPR($1)->bind_attribute_str(interp, REPR_PMC($1), $1, $2, $3, NO_HINT, $4);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_bind_attr_str on a SixModelObject");
}
/*
=item type_check(obj, wanted_type)
Checks if the given object's type accepts the checked type, using the
type check cache if one was published. Note, assumes that $3 is a type
object. If you're not sure that's what you have, call C<get_what> to
make sure.
=cut
*/
inline op type_check(out INT, in PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id && $3->vtable->base_type == smo_id) {
$1 = STABLE($2)->type_check(interp, $2, $3);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use type_check on two SixModelObjects");
}
}
/*
=item publish_type_check_cache(WHAT, type_list)
Publishes a type check cache, to be stored in the S-Table.
=cut
*/
inline op publish_type_check_cache(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == smo_id) {
STable *target_st = STABLE($1);
INTVAL items = VTABLE_elements(interp, $2);
if (items > 0) {
PMC **cache = mem_sys_allocate(sizeof(PMC *) * items);
INTVAL i;
for (i = 0; i < items; i++)
cache[i] = VTABLE_get_pmc_keyed_int(interp, $2, i);
target_st->type_check_cache = cache;
target_st->type_check_cache_length = items;
}
else {
target_st->type_check_cache = NULL;
target_st->type_check_cache_length = 0;
}
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"First argument to publish_type_check_cache must be a SixModelObject");
}
}
/*
=item publish_method_cache(WHAT, method_cache_hash)
Publishes a method dispatch cache - essentially, a set of name to code object
mappings.
=cut
*/
inline op publish_method_cache(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == smo_id) {
/* We copy the cache items to a Parrot hash to avoid making
* calls into the language's own hash implementation every
* time, which may be far more costly. */
STable *target_st = STABLE($1);
PMC *cache = pmc_new(interp, enum_class_Hash);
PMC *iter = VTABLE_get_iter(interp, $2);
while (VTABLE_get_bool(interp, iter)) {
STRING *name = VTABLE_shift_string(interp, iter);
PMC *meth = VTABLE_get_pmc_keyed_str(interp, $2, name);
VTABLE_set_pmc_keyed_str(interp, cache, name, meth);
}
target_st->method_cache = cache;
PARROT_GC_WRITE_BARRIER(interp, STABLE_PMC($1));
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"First argument to publish_method_cache must be a SixModelObject");
}
}
/*
=item repr_unbox_str()
Tries to unbox a native string using the REPR API.
=cut
*/
inline op repr_unbox_str(out STR, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->get_str(interp, REPR_PMC($2), $2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_unbox_str on a SixModelObject");
}
/*
=item repr_unbox_int()
Tries to unbox a native integer using the REPR API.
=cut
*/
inline op repr_unbox_int(out INT, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->get_int(interp, REPR_PMC($2), $2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_unbox_int on a SixModelObject");
}
/*
=item repr_unbox_num()
Tries to unbox a native floating pint number using the REPR API.
=cut
*/
inline op repr_unbox_num(out NUM, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = REPR($2)->get_num(interp, REPR_PMC($2), $2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_unbox_num on a SixModelObject");
}
/*
=item repr_box_str()
Box a native string to an instance of the specified type.
=cut
*/
inline op repr_box_str(out PMC, in STR, in PMC) :base_core {
if ($3->vtable->base_type == smo_id) {
$1 = REPR($3)->instance_of(interp, REPR_PMC($3), $3);
REPR($1)->set_str(interp, REPR_PMC($1), $1, $2);
}
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_box_str with a SixModelObject as the box target");
}
/*
=item repr_box_int()
Box a native int to an instance of the specified type.
=cut
*/
inline op repr_box_int(out PMC, in INT, in PMC) :base_core {
if ($3->vtable->base_type == smo_id) {
$1 = REPR($3)->instance_of(interp, REPR_PMC($3), $3);
REPR($1)->set_int(interp, REPR_PMC($1), $1, $2);
}
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_box_int with a SixModelObject as the box target");
}
/*
=item repr_box_num()
Box a native floating point number to an instance of the specified type.
=cut
*/
inline op repr_box_num(out PMC, in NUM, in PMC) :base_core {
if ($3->vtable->base_type == smo_id) {
$1 = REPR($3)->instance_of(interp, REPR_PMC($3), $3);
REPR($1)->set_num(interp, REPR_PMC($1), $1, $2);
}
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use repr_box_num with a SixModelObject as the box target");
}
/*
=item multi_dispatch_over_lexical_candidates()
Entry point to multi-dispatch over the current dispatchee list.
=cut
*/
inline op multi_dispatch_over_lexical_candidates(out PMC) :base_core {
INTVAL done = 0;
PMC *cur_ctx = CURRENT_CONTEXT(interp);
while (!PMC_IS_NULL(cur_ctx)) {
Parrot_Context *ctx_struct = CONTEXT_STRUCT(cur_ctx);
if (ctx_struct->current_sub->vtable->base_type == disp_id) {
/* Found a dispatcher. Enter the multi-dispatcher to find a the
* best candidate, if any. */
PMC *chosen = nqp_multi_dispatch(interp, ctx_struct->current_sub, cur_ctx);
if (!PMC_IS_NULL(chosen)) {
/* Invoke the chosen candidate using a copy of the original args. */
PMC *arg_copy = VTABLE_clone(interp, cur_ctx);
PMC *result;
PMC *prev_ctx = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
Parrot_pcc_invoke_from_sig_object(interp, chosen, arg_copy);
result = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), prev_ctx);
$1 = VTABLE_get_pmc_keyed_int(interp, result, 0);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Multiple dispatcher returned a null candidate.");
}
done = 1;
break;
}
cur_ctx = ctx_struct->outer_ctx;
}
if (!done)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"multi_dispatch_over_lexical_candidates was unable to find a candidate list");
}
/*
=item set_dispatchees()
Sets the dispatchees of the code object in $1, which must be a DispatcherSub,
to the array in $2.
=cut
*/
inline op set_dispatchees(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == disp_id)
PARROT_DISPATCHERSUB($1)->dispatchees = $2;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use set_dispatchees if first operand is a DispatcherSub.");
}
/*
=item create_dispatch_and_add_candidates()
Creates an instantiation of the dispatch routine (or proto, which may
serve as one) supplied and augments it with the provided candidates.
It relies on being passed the instantiation of the dispatcher from the
last outer scope that had an instantiation, and we thus take its
candidates.
$1 will be set to the new, augmented dispatch routine. $2 is the proto
or instantiation of it to clone. $3 is an array of candidates to add.
=cut
*/
inline op create_dispatch_and_add_candidates(out PMC, in PMC, in PMC) :base_core {
/* Ensure we have a dispatcher. */
if ($2->vtable->base_type == disp_id) {
/* Copy it (which makes an "instance") and add new candidates. */
PMC * copy = VTABLE_clone(interp, $2);
INTVAL i = 0;
INTVAL to_add = VTABLE_elements(interp, $3);
PARROT_DISPATCHERSUB(copy)->dispatchees = VTABLE_clone(interp,
PARROT_DISPATCHERSUB($2)->dispatchees);
for (i = 0; i < to_add; i++)
VTABLE_push_pmc(interp, PARROT_DISPATCHERSUB(copy)->dispatchees,
VTABLE_get_pmc_keyed_int(interp, $3, i));
$1 = copy;
}
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use create_dispatch_and_add_candidates if first operand is a DispatcherSub.");
}
/*
=item push_dispatchee()
Adds a single new code object to the end of a dispatcher's dispatchee list.
=cut
*/
inline op push_dispatchee(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == disp_id)
VTABLE_push_pmc(interp, PARROT_DISPATCHERSUB($1)->dispatchees, $2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use push_dispatchee if first operand is a DispatcherSub.");
}
/*
=item is_dispatcher()
Checks if a routine is considered a dispatcher (that is, if it has a
list of dispatchees).
=cut
*/
inline op is_dispatcher(out INT, in PMC) :base_core {
$1 = $2->vtable->base_type == disp_id &&
!PMC_IS_NULL(PARROT_DISPATCHERSUB($2)->dispatchees);
}
/*
=item set_sub_multisig()
Sets a sub's multi_signature. $1 is the sub, $2 is an array of type objects
and $3 is is an array of definedness constraint descriptors.
=cut
*/
inline op set_sub_multisig(in PMC, in PMC, in PMC) :base_core {
if ($1->vtable->base_type == enum_class_Sub) {
PMC *sig = Parrot_pmc_new(interp, ms_id);
SETATTR_NQPMultiSig_types(interp, sig, $2);
SETATTR_NQPMultiSig_definedness_constraints(interp, sig, $3);
SETATTR_Sub_multi_signature(interp, $1, sig);
PARROT_GC_WRITE_BARRIER(interp, $1);
}
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use set_sub_multisig if first operand is a Sub.");
}
/*
=item publish_vtable_mapping()
Publishes a Parrot v-table mapping, which will be hung off the s-table.
It's stored as an array, so lookups will be speedy.
=cut
*/
inline op stable_publish_vtable_mapping(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == smo_id) {
INTVAL i;
/* Get s-table and iterator over the mapping. */
STable *st = STABLE($1);
PMC *it = VTABLE_get_iter(interp, $2);
/* Toss any exist mapping array; allocate new one. */
if (st->parrot_vtable_mapping)
mem_sys_free(st->parrot_vtable_mapping);
st->parrot_vtable_mapping = mem_allocate_n_zeroed_typed(NUM_VTABLE_FUNCTIONS, PMC *);
/* Go through the various mapped names and insert them into the
* mapping table. */
while (VTABLE_get_bool(interp, it)) {
STRING *name = VTABLE_shift_string(interp, it);
char *c_name = Parrot_str_to_cstring(interp, name);
PMC *meth = VTABLE_get_pmc_keyed_str(interp, $2, name);
INTVAL idx = -1;
for (i = PARROT_VTABLE_LOW; i < NUM_VTABLE_FUNCTIONS; i++) {
if (strcmp(Parrot_vtable_slot_names[i], c_name) == 0) {
idx = i;
break;
}
}
if (idx >= 0)
st->parrot_vtable_mapping[idx] = meth;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"No such Parrot v-table '%Ss'", name);
}
}
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use stable_publish_vtable_mapping with a SixModelObject");
}
/*
=item nqp_get_sc_object()
Fetches an object from the serialization context keyed by handle and index.
=cut
*/
inline op nqp_get_sc_object(out PMC, in STR, in INT) :base_core {
PMC *sc = SC_get_sc(interp, $2);
if (!PMC_IS_NULL(sc))
$1 = VTABLE_get_pmc_keyed_int(interp, sc, $3);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot fetch object from non-existant serialization context %Ss",
$2);
}
/*
=item nqp_get_sc()
Gets the serialization context with the specified handle. Returns
NULL if it does not exist.
=cut
*/
inline op nqp_get_sc(out PMC, in STR) {
$1 = SC_get_sc(interp, $2);
}
/*
=item nqp_create_sc()
Creates a serialization context with the specified handle, adds it
to the registry and returns it.
=cut
*/
inline op nqp_create_sc(out PMC, in STR) {
$1 = pmc_new(interp, pmc_type(interp,
Parrot_str_new(interp, "SerializationContext", 0)));
VTABLE_set_string_native(interp, $1, $2);
SC_set_sc(interp, $2, $1);
}
/*
=item nqp_set_sc_object()
Stores an object in the serialization context keyed by handle and index.
=cut
*/
inline op nqp_set_sc_object(in STR, in INT, in PMC) :base_core {
PMC *sc = SC_get_sc(interp, $1);
if (!PMC_IS_NULL(sc))
VTABLE_set_pmc_keyed_int(interp, sc, $2, $3);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot fetch object from non-existant serialization context %Ss",
$1);
}
/*
=item nqp_set_sc_for_object()
Sets an object's serialization context.
=cut
*/
inline op nqp_set_sc_for_object(in PMC, in PMC) :base_core {
if ($1->vtable->base_type == smo_id)
SC_PMC($1) = $2;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use nqp_set_sc_for_object with a SixModelObject");
}
/*
=item nqp_get_sc_for_object()
Gets an object's serialization context. Returns NULL if there
is none.
=cut
*/
inline op nqp_get_sc_for_object(out PMC, in PMC) :base_core {
if ($2->vtable->base_type == smo_id)
$1 = SC_PMC($2);
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"Can only use nqp_get_sc_for_object with a SixModelObject");
}