-
Notifications
You must be signed in to change notification settings - Fork 67
/
MiscPrimitivePlugin.c
807 lines (738 loc) · 20.9 KB
/
MiscPrimitivePlugin.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
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
803
804
805
806
807
/* Automatically generated by
VMPluginCodeGenerator VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
from
MiscPrimitivePlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
*/
static char __buildInfo[] = "MiscPrimitivePlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1 " __DATE__ ;
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "sqMemoryAccess.h"
/*** Constants ***/
#define PrimErrBadArgument 3
#define PrimErrBadIndex 4
#define PrimErrNoModification 8
#define PrimErrUnsupported 7
/*** Function Prototypes ***/
EXPORT(const char*) getModuleName(void);
EXPORT(sqInt) primitiveCompareString(void);
EXPORT(sqInt) primitiveCompressToByteArray(void);
EXPORT(sqInt) primitiveConvert8BitSigned(void);
EXPORT(sqInt) primitiveDecompressFromByteArray(void);
EXPORT(sqInt) primitiveFindFirstInString(void);
EXPORT(sqInt) primitiveFindSubstring(void);
EXPORT(sqInt) primitiveIndexOfAsciiInString(void);
EXPORT(sqInt) primitiveStringHash(void);
EXPORT(sqInt) primitiveTranslateStringWithTable(void);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);
/*** Variables ***/
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static void * (*arrayValueOf)(sqInt oop);
static sqInt (*byteSizeOf)(sqInt oop);
static sqInt (*failed)(void);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*isBytes)(sqInt oop);
#if IMMUTABILITY
static sqInt (*isOopImmutable)(sqInt oop);
#endif /* IMMUTABILITY */
static sqInt (*methodArgumentCount)(void);
static sqInt (*methodReturnInteger)(sqInt integer);
static sqInt (*methodReturnReceiver)(void);
static sqInt (*pop)(sqInt nItems);
static sqInt (*primitiveFailFor)(sqInt reasonCode);
static sqInt (*sizeOfSTArrayFromCPrimitive)(void *cPtr);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern void * arrayValueOf(sqInt oop);
extern sqInt byteSizeOf(sqInt oop);
extern sqInt failed(void);
extern void * firstIndexableField(sqInt oop);
extern sqInt isBytes(sqInt oop);
#if IMMUTABILITY
#if IMMUTABILITY
extern sqInt isOopImmutable(sqInt oop);
#endif /* IMMUTABILITY */
#else
# define isOopImmutable(oop) 0
#endif
extern sqInt methodArgumentCount(void);
extern sqInt methodReturnInteger(sqInt integer);
extern sqInt methodReturnReceiver(void);
extern sqInt pop(sqInt nItems);
extern sqInt primitiveFailFor(sqInt reasonCode);
extern sqInt sizeOfSTArrayFromCPrimitive(void *cPtr);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern
#endif
struct VirtualMachine* interpreterProxy;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"MiscPrimitivePlugin VMMaker.oscog-eem.2480 (i)"
#else
"MiscPrimitivePlugin VMMaker.oscog-eem.2480 (e)"
#endif
;
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
/* InterpreterPlugin>>#getModuleName */
EXPORT(const char*)
getModuleName(void)
{
return moduleName;
}
/* ByteString (class) compare: string1 with: string2 collated: order */
/* MiscPrimitivePlugin>>#primitiveCompareString */
EXPORT(sqInt)
primitiveCompareString(void)
{
unsigned char c1;
unsigned char c2;
sqInt i;
sqInt len1;
sqInt len2;
unsigned char *order;
sqInt orderOop;
unsigned char *string1;
sqInt string1Oop;
unsigned char *string2;
sqInt string2Oop;
orderOop = stackValue(0);
string2Oop = stackValue(1);
string1Oop = stackValue(2);
if (!((isBytes(orderOop))
&& ((isBytes(string2Oop))
&& (isBytes(string1Oop))))) {
return primitiveFailFor(PrimErrBadArgument);
}
order = firstIndexableField(orderOop);
if ((sizeOfSTArrayFromCPrimitive(order)) < 256) {
return primitiveFailFor(PrimErrBadArgument);
}
string1 = firstIndexableField(string1Oop);
string2 = firstIndexableField(string2Oop);
len1 = sizeOfSTArrayFromCPrimitive(string1);
len2 = sizeOfSTArrayFromCPrimitive(string2);
for (i = 0; i < (((len1 < len2) ? len1 : len2)); i += 1) {
c1 = order[string1[i]];
c2 = order[string2[i]];
if (!(c1 == c2)) {
return methodReturnInteger((c1 < c2
? 1
: 3));
}
}
methodReturnInteger((len1 == len2
? 2
: (len1 < len2
? 1
: 3)));
return 0;
}
/* Bitmap compress: bm toByteArray: ba */
/* MiscPrimitivePlugin>>#primitiveCompressToByteArray */
EXPORT(sqInt)
primitiveCompressToByteArray(void)
{
unsigned char *ba;
int *bm;
sqInt destSize;
sqInt eqBytes;
sqInt i;
sqInt j;
sqInt j1;
sqInt j2;
sqInt j3;
sqInt j4;
sqInt j5;
sqInt j6;
sqInt j7;
sqInt k;
int lowByte;
sqInt m;
sqInt size;
int word;
bm = arrayValueOf(stackValue(1));
if (failed()) {
return null;
}
if (!(isBytes(stackValue(0)))) {
return primitiveFailFor(PrimErrBadArgument);
}
if (isOopImmutable(stackValue(0))) {
return primitiveFailFor(PrimErrNoModification);
}
ba = firstIndexableField(stackValue(0));
size = sizeOfSTArrayFromCPrimitive(bm);
destSize = sizeOfSTArrayFromCPrimitive(ba);
if (destSize < (((size * 4) + 7) + ((size / 0x7C0) * 3))) {
return primitiveFailFor(PrimErrUnsupported);
}
/* begin encodeInt:in:at: */
if (size <= 223) {
ba[0] = size;
i = 0 + 1;
goto l7;
}
if (size <= 7935) {
ba[0] = ((size / 256) + 224);
ba[0 + 1] = (size % 256);
i = 0 + 2;
goto l7;
}
ba[0] = 0xFF;
/* begin encodeBytesOf:in:at: */
for (j7 = 0; j7 <= 3; j7 += 1) {
ba[(0 + 1) + j7] = ((((usqInt) size) >> ((3 - j7) * 8)) & 0xFF);
}
i = (0 + 1) + 4;
l7: /* end encodeInt:in:at: */;
k = 0;
while (k < size) {
word = bm[k];
lowByte = word & 0xFF;
eqBytes = (((((usqInt) word) >> 8) & 0xFF) == lowByte)
&& ((((((usqInt) word) >> 16) & 0xFF) == lowByte)
&& (((((usqInt) word) >> 24) & 0xFF) == lowByte));
j = k;
while (((j + 1) < size)
&& (word == (bm[j + 1]))) {
j += 1;
}
if (j > k) {
if (eqBytes) {
/* begin encodeInt:in:at: */
if (((((j - k) + 1) * 4) + 1) <= 223) {
ba[i] = ((((j - k) + 1) * 4) + 1);
i += 1;
goto l1;
}
if (((((j - k) + 1) * 4) + 1) <= 7935) {
ba[i] = ((((((j - k) + 1) * 4) + 1) / 256) + 224);
ba[i + 1] = (((((j - k) + 1) * 4) + 1) % 256);
i += 2;
goto l1;
}
ba[i] = 0xFF;
/* begin encodeBytesOf:in:at: */
for (j1 = 0; j1 <= 3; j1 += 1) {
ba[(i + 1) + j1] = ((((usqInt) ((((j - k) + 1) * 4) + 1)) >> ((3 - j1) * 8)) & 0xFF);
}
i = (i + 1) + 4;
l1: /* end encodeInt:in:at: */;
ba[i] = lowByte;
i += 1;
}
else {
/* begin encodeInt:in:at: */
if (((((j - k) + 1) * 4) + 2) <= 223) {
ba[i] = ((((j - k) + 1) * 4) + 2);
i += 1;
goto l2;
}
if (((((j - k) + 1) * 4) + 2) <= 7935) {
ba[i] = ((((((j - k) + 1) * 4) + 2) / 256) + 224);
ba[i + 1] = (((((j - k) + 1) * 4) + 2) % 256);
i += 2;
goto l2;
}
ba[i] = 0xFF;
/* begin encodeBytesOf:in:at: */
for (j2 = 0; j2 <= 3; j2 += 1) {
ba[(i + 1) + j2] = ((((usqInt) ((((j - k) + 1) * 4) + 2)) >> ((3 - j2) * 8)) & 0xFF);
}
i = (i + 1) + 4;
l2: /* end encodeInt:in:at: */;
/* begin encodeBytesOf:in:at: */
for (j3 = 0; j3 <= 3; j3 += 1) {
ba[i + j3] = ((((usqInt) word) >> ((3 - j3) * 8)) & 0xFF);
}
i += 4;
}
k = j + 1;
}
else {
if (eqBytes) {
/* begin encodeInt:in:at: */
if (((1 * 4) + 1) <= 223) {
ba[i] = ((1 * 4) + 1);
i += 1;
goto l4;
}
if (((1 * 4) + 1) <= 7935) {
ba[i] = ((((1 * 4) + 1) / 256) + 224);
ba[i + 1] = (((1 * 4) + 1) % 256);
i += 2;
goto l4;
}
ba[i] = 0xFF;
/* begin encodeBytesOf:in:at: */
for (j4 = 0; j4 <= 3; j4 += 1) {
ba[(i + 1) + j4] = ((((usqInt) ((1 * 4) + 1)) >> ((3 - j4) * 8)) & 0xFF);
}
i = (i + 1) + 4;
l4: /* end encodeInt:in:at: */;
ba[i] = lowByte;
i += 1;
k += 1;
}
else {
while (((j + 1) < size)
&& ((bm[j]) != (bm[j + 1]))) {
j += 1;
}
if ((j + 1) == size) {
j += 1;
}
/* begin encodeInt:in:at: */
if ((((j - k) * 4) + 3) <= 223) {
ba[i] = (((j - k) * 4) + 3);
i += 1;
goto l6;
}
if ((((j - k) * 4) + 3) <= 7935) {
ba[i] = (((((j - k) * 4) + 3) / 256) + 224);
ba[i + 1] = ((((j - k) * 4) + 3) % 256);
i += 2;
goto l6;
}
ba[i] = 0xFF;
/* begin encodeBytesOf:in:at: */
for (j6 = 0; j6 <= 3; j6 += 1) {
ba[(i + 1) + j6] = ((((usqInt) (((j - k) * 4) + 3)) >> ((3 - j6) * 8)) & 0xFF);
}
i = (i + 1) + 4;
l6: /* end encodeInt:in:at: */;
for (m = k; m < j; m += 1) {
/* begin encodeBytesOf:in:at: */
for (j5 = 0; j5 <= 3; j5 += 1) {
ba[i + j5] = ((((usqInt) (bm[m])) >> ((3 - j5) * 8)) & 0xFF);
}
i += 4;
}
k = j;
}
}
}
methodReturnInteger(i);
return 0;
}
/* SampledSound (class) convert8bitSignedFrom: aByteArray to16Bit:
aSoundBuffer
*/
/* MiscPrimitivePlugin>>#primitiveConvert8BitSigned */
EXPORT(sqInt)
primitiveConvert8BitSigned(void)
{
unsigned char *aByteArray;
sqInt arraySize;
unsigned short *aSoundBuffer;
sqInt byteArrayOop;
sqInt i;
unsigned char s;
sqInt soundBufferOop;
byteArrayOop = stackValue(1);
if (!(isBytes(byteArrayOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
aByteArray = firstIndexableField(byteArrayOop);
soundBufferOop = stackValue(0);
aSoundBuffer = arrayValueOf(soundBufferOop);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
if (isOopImmutable(soundBufferOop)) {
return primitiveFailFor(PrimErrNoModification);
}
arraySize = sizeOfSTArrayFromCPrimitive(aByteArray);
if ((byteSizeOf(soundBufferOop)) < (2 * arraySize)) {
return primitiveFailFor(PrimErrBadArgument);
}
for (i = 0; i < arraySize; i += 1) {
s = aByteArray[i];
aSoundBuffer[i] = ((s > 0x7F
? ((usqInt) (s - 256) << 8)
: ((usqInt) s << 8)));
}
methodReturnReceiver();
return 0;
}
/* Bitmap decompress: bm fromByteArray: ba at: index */
/* MiscPrimitivePlugin>>#primitiveDecompressFromByteArray */
EXPORT(sqInt)
primitiveDecompressFromByteArray(void)
{
unsigned int anInt;
unsigned char *ba;
int *bm;
unsigned int code;
unsigned int data;
sqInt end;
sqInt i;
sqInt index;
sqInt j;
sqInt k;
sqInt m;
unsigned int n;
sqInt pastEnd;
bm = arrayValueOf(stackValue(2));
if (isOopImmutable(stackValue(2))) {
return primitiveFailFor(PrimErrNoModification);
}
if (!(isBytes(stackValue(1)))) {
return primitiveFailFor(PrimErrBadArgument);
}
ba = firstIndexableField(stackValue(1));
index = stackIntegerValue(0);
if (failed()) {
return null;
}
i = index - 1;
k = 0;
end = sizeOfSTArrayFromCPrimitive(ba);
pastEnd = sizeOfSTArrayFromCPrimitive(bm);
while (i < end) {
anInt = ba[i];
i += 1;
if (!(anInt <= 223)) {
if (anInt <= 0xFE) {
anInt = ((anInt - 224) * 256) + (ba[i]);
i += 1;
}
else {
anInt = 0;
for (j = 1; j <= 4; j += 1) {
anInt = (((usqInt) anInt << 8)) + (ba[i]);
i += 1;
}
}
}
n = ((usqInt) anInt) >> 2;
if ((k + n) > pastEnd) {
return primitiveFailFor(PrimErrBadIndex);
}
/* code = 0 ifTrue: [nil]. */
code = anInt & 3;
if (code == 1) {
data = ba[i];
i += 1;
data = data | (((usqInt) data << 8));
data = data | (((usqInt) data << 16));
for (j = 1; j <= n; j += 1) {
bm[k] = data;
k += 1;
}
}
if (code == 2) {
data = 0;
for (j = 1; j <= 4; j += 1) {
data = (((usqInt) data << 8)) | (ba[i]);
i += 1;
}
for (j = 1; j <= n; j += 1) {
bm[k] = data;
k += 1;
}
}
if (code == 3) {
for (m = 1; m <= n; m += 1) {
data = 0;
for (j = 1; j <= 4; j += 1) {
data = (((usqInt) data << 8)) | (ba[i]);
i += 1;
}
bm[k] = data;
k += 1;
}
}
}
pop(methodArgumentCount());
return 0;
}
/* ByteString (class) findFirstInString: aString inSet: inclusionMap
startingAt: start */
/* MiscPrimitivePlugin>>#primitiveFindFirstInString */
EXPORT(sqInt)
primitiveFindFirstInString(void)
{
unsigned char *aString;
sqInt aStringOop;
sqInt i;
unsigned char *inclusionMap;
sqInt inclusionMapOop;
sqInt stringSize;
aStringOop = stackValue(2);
if (!(isBytes(aStringOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
inclusionMapOop = stackValue(1);
if (!(isBytes(inclusionMapOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
i = stackIntegerValue(0);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
/* Convert to 0-based index. */
i -= 1;
if (i < 0) {
return primitiveFailFor(PrimErrBadIndex);
}
inclusionMap = firstIndexableField(inclusionMapOop);
if ((sizeOfSTArrayFromCPrimitive(inclusionMap)) != 256) {
return methodReturnInteger(0);
}
aString = firstIndexableField(aStringOop);
stringSize = sizeOfSTArrayFromCPrimitive(aString);
while ((i < stringSize)
&& ((inclusionMap[aString[i]]) == 0)) {
i += 1;
}
methodReturnInteger((i >= stringSize
? 0
: i + 1));
return 0;
}
/* ByteString findSubstring: key in: body startingAt: start matchTable:
matchTable
*/
/* MiscPrimitivePlugin>>#primitiveFindSubstring */
EXPORT(sqInt)
primitiveFindSubstring(void)
{
unsigned char *body;
sqInt bodyOop;
sqInt index;
unsigned char *key;
sqInt keyOop;
sqInt keySize;
unsigned char *matchTable;
sqInt matchTableOop;
sqInt start;
sqInt startIndex;
sqInt startIndexLimiT;
keyOop = stackValue(3);
if (!(isBytes(keyOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
bodyOop = stackValue(2);
if (!(isBytes(bodyOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
start = stackIntegerValue(1);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
matchTableOop = stackValue(0);
if (!(isBytes(matchTableOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
matchTable = firstIndexableField(matchTableOop);
if ((sizeOfSTArrayFromCPrimitive(matchTable)) < 256) {
return primitiveFailFor(PrimErrBadArgument);
}
key = firstIndexableField(keyOop);
if (((keySize = sizeOfSTArrayFromCPrimitive(key))) > 0) {
/* adjust for zero relative indexes */
keySize -= 1;
/* adjust for zero relative indexes */
start = (((start - 1) < 0) ? 0 : (start - 1));
body = firstIndexableField(bodyOop);
for (startIndex = start, startIndexLimiT = (((sizeOfSTArrayFromCPrimitive(body)) - 1) - keySize); startIndex <= startIndexLimiT; startIndex += 1) {
index = 0;
while ((matchTable[body[startIndex + index]]) == (matchTable[key[index]])) {
if (index == keySize) {
return methodReturnInteger(startIndex + 1);
}
index += 1;
}
}
}
return methodReturnInteger(0);
}
/* ByteString indexOfAscii: anInteger inString: aString startingAt: start */
/* MiscPrimitivePlugin>>#primitiveIndexOfAsciiInString */
EXPORT(sqInt)
primitiveIndexOfAsciiInString(void)
{
sqInt anInteger;
unsigned char *aString;
sqInt aStringOop;
sqInt pos;
sqInt start;
sqInt stringSize;
anInteger = stackIntegerValue(2);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
aStringOop = stackValue(1);
if (!(isBytes(aStringOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
start = stackIntegerValue(0);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
if (!(start >= 1)) {
return primitiveFailFor(PrimErrBadIndex);
}
aString = firstIndexableField(aStringOop);
stringSize = sizeOfSTArrayFromCPrimitive(aString);
for (pos = (start - 1); pos < stringSize; pos += 1) {
if ((aString[pos]) == anInteger) {
return methodReturnInteger(pos + 1);
}
}
return methodReturnInteger(0);
}
/* ByteArray (class) hashBytes: aByteArray startingWith: speciesHash */
/* MiscPrimitivePlugin>>#primitiveStringHash */
EXPORT(sqInt)
primitiveStringHash(void)
{
unsigned char *aByteArray;
sqInt byteArrayOop;
unsigned int hash;
sqInt pos;
sqInt posLimiT;
hash = stackIntegerValue(0);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
byteArrayOop = stackValue(1);
if (!(isBytes(byteArrayOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
aByteArray = firstIndexableField(byteArrayOop);
for (pos = 0, posLimiT = ((sizeOfSTArrayFromCPrimitive(aByteArray)) - 1); pos <= posLimiT; pos += 1) {
hash = (hash + (aByteArray[pos])) * 1664525;
}
methodReturnInteger(hash & 0xFFFFFFF);
return 0;
}
/* ByteString (class) translate: aString from: start to: stop table: table */
/* MiscPrimitivePlugin>>#primitiveTranslateStringWithTable */
EXPORT(sqInt)
primitiveTranslateStringWithTable(void)
{
unsigned char *aString;
sqInt aStringOop;
sqInt i;
sqInt start;
sqInt stop;
unsigned char *table;
sqInt tableOop;
aStringOop = stackValue(3);
if (!(isBytes(aStringOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
if (isOopImmutable(aStringOop)) {
return primitiveFailFor(PrimErrNoModification);
}
start = stackIntegerValue(2);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
stop = stackIntegerValue(1);
if (failed()) {
return primitiveFailFor(PrimErrBadArgument);
}
tableOop = stackValue(0);
if (!(isBytes(tableOop))) {
return primitiveFailFor(PrimErrBadArgument);
}
aString = firstIndexableField(aStringOop);
if (!((start >= 1)
&& (stop <= (sizeOfSTArrayFromCPrimitive(aString))))) {
return primitiveFailFor(PrimErrBadIndex);
}
table = firstIndexableField(tableOop);
if ((sizeOfSTArrayFromCPrimitive(table)) < 256) {
return primitiveFailFor(PrimErrBadArgument);
}
for (i = (start - 1); i < stop; i += 1) {
aString[i] = (table[aString[i]]);
}
methodReturnReceiver();
return 0;
}
/* Note: This is coded so that it can be run in Squeak. */
/* InterpreterPlugin>>#setInterpreter: */
EXPORT(sqInt)
setInterpreter(struct VirtualMachine *anInterpreter)
{
sqInt ok;
interpreterProxy = anInterpreter;
ok = ((interpreterProxy->majorVersion()) == (VM_PROXY_MAJOR))
&& ((interpreterProxy->minorVersion()) >= (VM_PROXY_MINOR));
if (ok) {
#if !defined(SQUEAK_BUILTIN_PLUGIN)
arrayValueOf = interpreterProxy->arrayValueOf;
byteSizeOf = interpreterProxy->byteSizeOf;
failed = interpreterProxy->failed;
firstIndexableField = interpreterProxy->firstIndexableField;
isBytes = interpreterProxy->isBytes;
#if IMMUTABILITY
isOopImmutable = interpreterProxy->isOopImmutable;
#else
#if !defined(isOopImmutable)
isOopImmutable = 0;
#endif
#endif
methodArgumentCount = interpreterProxy->methodArgumentCount;
methodReturnInteger = interpreterProxy->methodReturnInteger;
methodReturnReceiver = interpreterProxy->methodReturnReceiver;
pop = interpreterProxy->pop;
primitiveFailFor = interpreterProxy->primitiveFailFor;
sizeOfSTArrayFromCPrimitive = interpreterProxy->sizeOfSTArrayFromCPrimitive;
stackIntegerValue = interpreterProxy->stackIntegerValue;
stackValue = interpreterProxy->stackValue;
#endif /* !defined(SQUEAK_BUILTIN_PLUGIN) */
}
return ok;
}
#ifdef SQUEAK_BUILTIN_PLUGIN
static char _m[] = "MiscPrimitivePlugin";
void* MiscPrimitivePlugin_exports[][3] = {
{(void*)_m, "getModuleName", (void*)getModuleName},
{(void*)_m, "primitiveCompareString\000\002", (void*)primitiveCompareString},
{(void*)_m, "primitiveCompressToByteArray\000\377", (void*)primitiveCompressToByteArray},
{(void*)_m, "primitiveConvert8BitSigned\000\002", (void*)primitiveConvert8BitSigned},
{(void*)_m, "primitiveDecompressFromByteArray\000\000", (void*)primitiveDecompressFromByteArray},
{(void*)_m, "primitiveFindFirstInString\000\002", (void*)primitiveFindFirstInString},
{(void*)_m, "primitiveFindSubstring\000\002", (void*)primitiveFindSubstring},
{(void*)_m, "primitiveIndexOfAsciiInString\000\002", (void*)primitiveIndexOfAsciiInString},
{(void*)_m, "primitiveStringHash\000\001", (void*)primitiveStringHash},
{(void*)_m, "primitiveTranslateStringWithTable\000\001", (void*)primitiveTranslateStringWithTable},
{(void*)_m, "setInterpreter", (void*)setInterpreter},
{NULL, NULL, NULL}
};
#else /* ifdef SQ_BUILTIN_PLUGIN */
EXPORT(signed char) primitiveCompareStringAccessorDepth = 2;
EXPORT(signed char) primitiveConvert8BitSignedAccessorDepth = 2;
EXPORT(signed char) primitiveDecompressFromByteArrayAccessorDepth = 0;
EXPORT(signed char) primitiveFindFirstInStringAccessorDepth = 2;
EXPORT(signed char) primitiveFindSubstringAccessorDepth = 2;
EXPORT(signed char) primitiveIndexOfAsciiInStringAccessorDepth = 2;
EXPORT(signed char) primitiveStringHashAccessorDepth = 1;
EXPORT(signed char) primitiveTranslateStringWithTableAccessorDepth = 1;
#endif /* ifdef SQ_BUILTIN_PLUGIN */