codefrau / SqueakJS

A Squeak Smalltalk VM in Javascript
https://squeak.js.org
MIT License
364 stars 75 forks source link

Built-in primitives do not use proper error codes #162

Open LinqLover opened 3 months ago

LinqLover commented 3 months ago

Forgive me if this statement is not true in its generality, but I found at least a couple of occurrences. For example, the perform primitives do not emit proper error codes at the moment such as #'bad number of arguments' or #'bad argument'. This is inconsistent with the OSVM and the image-side simulator and, if I did not overlook anything, the last reason after my recent PRs that keeps all the ContextTests from going green.

Would this be desired? Would this pattern be the right approach?

    primitivePerform: function(argCount) {
        var selector = this.stackValue(argCount-1);
        var rcvr = this.stackValue(argCount);
        var trueArgCount = argCount - 1;
        var entry = this.findSelectorInClass(selector, trueArgCount, this.getClass(rcvr), argCount);
        if (entry.selector === selector) {
            // selector has been found, rearrange stack
-             if (entry.argCount !== trueArgCount)
+             if (entry.argCount !== trueArgCount) {
+               this.vm.primFailCode = Squeak.PrimErrBadNumArgs;
                return false;
+             }
        var stack = this.activeContext.pointers; // slide eveything down...
            var selectorIndex = this.sp - trueArgCount;
        this.arrayCopy(stack, selectorIndex+1, stack, selectorIndex, trueArgCount);
        this.sp--; // adjust sp accordingly
        } else {
            // stack has already been arranged for #doesNotUnderstand:/#cannotInterpret:
            rcvr = this.stackValue(entry.argCount);
        }
        this.executeNewMethod(rcvr, entry.method, entry.argCount, entry.primIndex, entry.mClass, selector);
        return true;
    },
codefrau commented 3 months ago

Yes, this would be desirable, and this looks like the right way to do it ☺️