Spaces:
Running
Running
File size: 15,195 Bytes
8f3f8db |
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 |
/* Smalltalk from Squeak4.5 with VMMaker 4.13.6 translated as JS source on 3 November 2014 1:52:23 pm */
/* Automatically generated by
JSPluginCodeGenerator VMMakerJS-bf.15 uuid: fd4e10f2-3773-4e80-8bb5-c4b471a014e5
from
MiscPrimitivePlugin VMMaker-bf.353 uuid: 8ae25e7e-8d2c-451e-8277-598b30e9c002
*/
(function MiscPrimitivePlugin() {
"use strict";
var VM_PROXY_MAJOR = 1;
var VM_PROXY_MINOR = 11;
/*** Functions ***/
function CLASSOF(obj) { return typeof obj === "number" ? interpreterProxy.classSmallInteger() : obj.sqClass }
function SIZEOF(obj) { return obj.pointers ? obj.pointers.length : obj.words ? obj.words.length : obj.bytes ? obj.bytes.length : 0 }
function BYTESIZEOF(obj) { return obj.bytes ? obj.bytes.length : obj.words ? obj.words.length * 4 : 0 }
function DIV(a, b) { return Math.floor(a / b) | 0; } // integer division
function MOD(a, b) { return a - DIV(a, b) * b | 0; } // signed modulus
function SHL(a, b) { return b > 31 ? 0 : a << b; } // fix JS shift
function SHR(a, b) { return b > 31 ? 0 : a >>> b; } // fix JS shift
function SHIFT(a, b) { return b < 0 ? (b < -31 ? 0 : a >>> (0-b) ) : (b > 31 ? 0 : a << b); }
/*** Variables ***/
var interpreterProxy = null;
var moduleName = "MiscPrimitivePlugin 3 November 2014 (e)";
/* Copy the integer anInt into byteArray ba at index i, and return the next index */
function encodeBytesOfinat(anInt, ba, i) {
var j;
for (j = 0; j <= 3; j++) {
ba[(i + j) - 1] = ((SHR(anInt, ((3 - j) * 8))) & 255);
}
return i + 4;
}
/* Encode the integer anInt in byteArray ba at index i, and return the next index.
The encoding is as follows...
0-223 0-223
224-254 (0-30)*256 + next byte (0-7935)
255 next 4 bytes */
function encodeIntinat(anInt, ba, i) {
if (anInt <= 223) {
ba[i - 1] = anInt;
return i + 1;
}
if (anInt <= 7935) {
ba[i - 1] = ((anInt >> 8) + 224);
ba[i] = (MOD(anInt, 256));
return i + 2;
}
ba[i - 1] = 255;
return encodeBytesOfinat(anInt, ba, i + 1);
}
/* Note: This is coded so that plugins can be run from Squeak. */
function getInterpreter() {
return interpreterProxy;
}
/* 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! */
function getModuleName() {
return moduleName;
}
function halt() {
;
}
function msg(s) {
console.log(moduleName + ": " + s);
}
/* Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array. */
function primitiveCompareString(argCount) {
var rcvr;
var string1;
var string2;
var order;
var c1;
var c2;
var i;
var len1;
var len2;
rcvr = interpreterProxy.stackValue(3);
string1 = interpreterProxy.stackBytes(2);
string2 = interpreterProxy.stackBytes(1);
order = interpreterProxy.stackBytes(0);
if (interpreterProxy.failed()) {
return null;
}
len1 = string1.length;
len2 = string2.length;
for (i = 1; i <= Math.min(len1, len2); i++) {
c1 = order[string1[i - 1]];
c2 = order[string2[i - 1]];
if (c1 !== c2) {
if (c1 < c2) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 1);
return null;
} else {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 3);
return null;
}
}
}
if (len1 === len2) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 2);
return null;
}
if (len1 < len2) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 1);
return null;
} else {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 3);
return null;
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.pop(argCount);
}
/* Store a run-coded compression of the receiver into the byteArray ba,
and return the last index stored into. ba is assumed to be large enough.
The encoding is as follows...
S {N D}*.
S is the size of the original bitmap, followed by run-coded pairs.
N is a run-length * 4 + data code.
D, the data, depends on the data code...
0 skip N words, D is absent
1 N words with all 4 bytes = D (1 byte)
2 N words all = D (4 bytes)
3 N words follow in D (4N bytes)
S and N are encoded as follows...
0-223 0-223
224-254 (0-30)*256 + next byte (0-7935)
255 next 4 bytes */
function primitiveCompressToByteArray(argCount) {
var rcvr;
var bm;
var ba;
var eqBytes;
var i;
var j;
var k;
var lowByte;
var m;
var size;
var word;
rcvr = interpreterProxy.stackValue(2);
bm = interpreterProxy.stackInt32Array(1);
ba = interpreterProxy.stackBytes(0);
if (interpreterProxy.failed()) {
return null;
}
size = bm.length;
i = encodeIntinat(size, ba, 1);
k = 1;
while (k <= size) {
word = bm[k - 1];
lowByte = word & 255;
eqBytes = (((word >>> 8) & 255) === lowByte) && ((((word >>> 16) & 255) === lowByte) && (((word >>> 24) & 255) === lowByte));
j = k;
while ((j < size) && (word === bm[j])) {
++j;
}
if (j > k) {
/* We have two or more = words, ending at j */
if (eqBytes) {
/* Actually words of = bytes */
i = encodeIntinat((((j - k) + 1) * 4) + 1, ba, i);
ba[i - 1] = lowByte;
++i;
} else {
i = encodeIntinat((((j - k) + 1) * 4) + 2, ba, i);
i = encodeBytesOfinat(word, ba, i);
}
k = j + 1;
} else {
/* Check for word of 4 = bytes */
if (eqBytes) {
/* Note 1 word of 4 = bytes */
i = encodeIntinat((1 * 4) + 1, ba, i);
ba[i - 1] = lowByte;
++i;
++k;
} else {
/* Finally, check for junk */
while ((j < size) && (bm[j - 1] !== bm[j])) {
++j;
}
if (j === size) {
++j;
}
i = encodeIntinat(((j - k) * 4) + 3, ba, i);
for (m = k; m <= (j - 1); m++) {
i = encodeBytesOfinat(bm[m - 1], ba, i);
}
k = j;
}
}
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, i - 1);
return null;
}
/* Copy the contents of the given array of signed 8-bit samples into the given array of 16-bit signed samples. */
function primitiveConvert8BitSigned(argCount) {
var rcvr;
var aByteArray;
var aSoundBuffer;
var i;
var n;
var s;
rcvr = interpreterProxy.stackValue(2);
aByteArray = interpreterProxy.stackBytes(1);
aSoundBuffer = interpreterProxy.stackUint16Array(0);
if (interpreterProxy.failed()) {
return null;
}
n = aByteArray.length;
for (i = 1; i <= n; i++) {
s = aByteArray[i - 1];
if (s > 127) {
aSoundBuffer[i - 1] = ((s - 256) << 8);
} else {
aSoundBuffer[i - 1] = (s << 8);
}
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.pop(argCount);
}
/* Decompress the body of a byteArray encoded by compressToByteArray (qv)...
The format is simply a sequence of run-coded pairs, {N D}*.
N is a run-length * 4 + data code.
D, the data, depends on the data code...
0 skip N words, D is absent
(could be used to skip from one raster line to the next)
1 N words with all 4 bytes = D (1 byte)
2 N words all = D (4 bytes)
3 N words follow in D (4N bytes)
S and N are encoded as follows (see decodeIntFrom:)...
0-223 0-223
224-254 (0-30)*256 + next byte (0-7935)
255 next 4 bytes */
/* NOTE: If fed with garbage, this routine could read past the end of ba, but it should fail before writing past the ned of bm. */
function primitiveDecompressFromByteArray(argCount) {
var rcvr;
var bm;
var ba;
var index;
var anInt;
var code;
var data;
var end;
var i;
var j;
var k;
var m;
var n;
var pastEnd;
rcvr = interpreterProxy.stackValue(3);
bm = interpreterProxy.stackInt32Array(2);
ba = interpreterProxy.stackBytes(1);
index = interpreterProxy.stackIntegerValue(0);
if (interpreterProxy.failed()) {
return null;
}
/* byteArray read index */
i = index;
end = ba.length;
/* bitmap write index */
k = 1;
pastEnd = bm.length + 1;
while (i <= end) {
/* Decode next run start N */
anInt = ba[i - 1];
++i;
if (!(anInt <= 223)) {
if (anInt <= 254) {
anInt = ((anInt - 224) * 256) + ba[i - 1];
++i;
} else {
anInt = 0;
for (j = 1; j <= 4; j++) {
anInt = (anInt << 8) + ba[i - 1];
++i;
}
}
}
n = anInt >>> 2;
if ((k + n) > pastEnd) {
interpreterProxy.primitiveFail();
return null;
}
code = anInt & 3;
if (code === 0) {
/* skip */
null;
}
if (code === 1) {
/* n consecutive words of 4 bytes = the following byte */
data = ba[i - 1];
++i;
data = data | (data << 8);
data = data | (data << 16);
for (j = 1; j <= n; j++) {
bm[k - 1] = data;
++k;
}
}
if (code === 2) {
/* n consecutive words = 4 following bytes */
data = 0;
for (j = 1; j <= 4; j++) {
data = (data << 8) | ba[i - 1];
++i;
}
for (j = 1; j <= n; j++) {
bm[k - 1] = data;
++k;
}
}
if (code === 3) {
/* n consecutive words from the data... */
for (m = 1; m <= n; m++) {
data = 0;
for (j = 1; j <= 4; j++) {
data = (data << 8) | ba[i - 1];
++i;
}
bm[k - 1] = data;
++k;
}
}
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.pop(argCount);
}
function primitiveFindFirstInString(argCount) {
var rcvr;
var aString;
var inclusionMap;
var start;
var i;
var stringSize;
rcvr = interpreterProxy.stackValue(3);
aString = interpreterProxy.stackBytes(2);
inclusionMap = interpreterProxy.stackBytes(1);
start = interpreterProxy.stackIntegerValue(0);
if (interpreterProxy.failed()) {
return null;
}
if (inclusionMap.length !== 256) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 0);
return null;
}
i = start;
stringSize = aString.length;
while ((i <= stringSize) && (inclusionMap[aString[i - 1]] === 0)) {
++i;
}
if (i > stringSize) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 0);
return null;
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, i);
return null;
}
/* Answer the index in the string body at which the substring key first occurs, at or beyond start. The match is determined using matchTable, which can be used to effect, eg, case-insensitive matches. If no match is found, zero will be returned.
The algorithm below is not optimum -- it is intended to be translated to C which will go so fast that it wont matter. */
function primitiveFindSubstring(argCount) {
var rcvr;
var key;
var body;
var start;
var matchTable;
var index;
var startIndex;
rcvr = interpreterProxy.stackValue(4);
key = interpreterProxy.stackBytes(3);
body = interpreterProxy.stackBytes(2);
start = interpreterProxy.stackIntegerValue(1);
matchTable = interpreterProxy.stackBytes(0);
if (interpreterProxy.failed()) {
return null;
}
if (key.length === 0) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 0);
return null;
}
for (startIndex = start; startIndex <= ((body.length - key.length) + 1); startIndex++) {
index = 1;
while (matchTable[body[((startIndex + index) - 1) - 1]] === matchTable[key[index - 1]]) {
if (index === key.length) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, startIndex);
return null;
}
++index;
}
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 0);
return null;
}
function primitiveIndexOfAsciiInString(argCount) {
var rcvr;
var anInteger;
var aString;
var start;
var pos;
var stringSize;
rcvr = interpreterProxy.stackValue(3);
anInteger = interpreterProxy.stackIntegerValue(2);
aString = interpreterProxy.stackBytes(1);
start = interpreterProxy.stackIntegerValue(0);
if (interpreterProxy.failed()) {
return null;
}
stringSize = aString.length;
for (pos = start; pos <= stringSize; pos++) {
if (aString[pos - 1] === anInteger) {
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, pos);
return null;
}
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, 0);
return null;
}
/* Answer the hash of a byte-indexed collection,
using speciesHash as the initial value.
See SmallInteger>>hashMultiply.
The primitive should be renamed at a
suitable point in the future */
function primitiveStringHash(argCount) {
var rcvr;
var aByteArray;
var speciesHash;
var byteArraySize;
var hash;
var low;
var pos;
rcvr = interpreterProxy.stackValue(2);
aByteArray = interpreterProxy.stackBytes(1);
speciesHash = interpreterProxy.stackIntegerValue(0);
if (interpreterProxy.failed()) {
return null;
}
byteArraySize = aByteArray.length;
hash = speciesHash & 268435455;
for (pos = 1; pos <= byteArraySize; pos++) {
/* Begin hashMultiply */
hash += aByteArray[pos - 1];
low = hash & 16383;
hash = ((9741 * low) + ((((9741 * (hash >>> 14)) + (101 * low)) & 16383) * 16384)) & 268435455;
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.popthenPush(argCount + 1, hash);
return null;
}
/* translate the characters in the string by the given table, in place */
function primitiveTranslateStringWithTable(argCount) {
var rcvr;
var aString;
var start;
var stop;
var table;
var i;
rcvr = interpreterProxy.stackValue(4);
aString = interpreterProxy.stackBytes(3);
start = interpreterProxy.stackIntegerValue(2);
stop = interpreterProxy.stackIntegerValue(1);
table = interpreterProxy.stackBytes(0);
if (interpreterProxy.failed()) {
return null;
}
for (i = start; i <= stop; i++) {
aString[i - 1] = table[aString[i - 1]];
}
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.pop(argCount);
}
/* Note: This is coded so that is can be run from Squeak. */
function setInterpreter(anInterpreter) {
var ok;
interpreterProxy = anInterpreter;
ok = interpreterProxy.majorVersion() == VM_PROXY_MAJOR;
if (ok === false) {
return false;
}
ok = interpreterProxy.minorVersion() >= VM_PROXY_MINOR;
return ok;
}
function registerPlugin() {
if (typeof Squeak === "object" && Squeak.registerExternalModule) {
Squeak.registerExternalModule("MiscPrimitivePlugin", {
primitiveConvert8BitSigned: primitiveConvert8BitSigned,
primitiveCompareString: primitiveCompareString,
primitiveTranslateStringWithTable: primitiveTranslateStringWithTable,
primitiveStringHash: primitiveStringHash,
primitiveCompressToByteArray: primitiveCompressToByteArray,
primitiveFindSubstring: primitiveFindSubstring,
primitiveIndexOfAsciiInString: primitiveIndexOfAsciiInString,
setInterpreter: setInterpreter,
primitiveDecompressFromByteArray: primitiveDecompressFromByteArray,
getModuleName: getModuleName,
primitiveFindFirstInString: primitiveFindFirstInString,
});
} else self.setTimeout(registerPlugin, 100);
}
registerPlugin();
})(); // Register module/plugin
|