[Vm-dev] SqueakJS named primitives in the interpreter module
David T. Lewis
lewis at mail.msen.com
Sat Jan 21 04:06:38 UTC 2017
On Thu, Jan 19, 2017 at 02:19:25PM +0100, Bert Freudenberg wrote:
>
> On Thu, Jan 19, 2017 at 3:55 AM, David T. Lewis <lewis at mail.msen.com> wrote:
>
> >
> > For named primitives, we can load and run a primitive function from a specific
> > module, as in the case of <primitive: 'primitiveFileSize' module: 'FilePlugin'>.
> >
> > We can also load a primitive with module name unspecified, as in the case of
> > <primitive: 'primitiveScreenDepth'>. In that case we load and run the function
> > from the main interpreter module.
> >
> > I want to implement the anonymous named primitive #primitiveUtcWithOffset in
> > SqueakJS, but I am a total JavaScript noob so I am looking for an example of
> > some other anonymous named primitive to use as a pattern.
> >
> > At first glance, I do not see any anonymous named primitives implemented
> > in vm.js,
> >
>
> Indeed, SqueakJS does not implement any of the currently used module-less
> named primitives.
>
> It does, however, implement some obsolete ones, e.g. '
> m23PrimitiveTransformPoint' which was later moved to Matrix2x3Plugin.
>
> The lookup is quite trivial: If you specify an empty module name, it just
> looks up the method in the VM's primitive handler class itself, instead of
> a named module. E.g. in SqueakJS you could call the beep primitive either
> by index or by name, <primitive: 140> works as well as <primitive:
> 'primitiveBeep'>.
>
>
> > so I want to ask - does the basic functionality of looking up and
> > executing e.g.
> > #primitiveScreenDepth from the <primitive: 'primitiveScreenDepth'> pragma
> > work?
> >
>
> Yes. Simply name your JavaScript method 'primitiveUtcWithOffset' and put it
> in the 'Squeak.Primitives' class. I'd suggest putting it in the 'time'
> section https://github.com/bertfreudenberg/SqueakJS/blob/
> 432dafd615a8e48019dbfc5e62a6599e4648ca3b/vm.js#L6486
>
> Note that quite a few indexed primitives do not exist as a full method.
> E.g. prim 135 is currently implemented as
>
> case 135: return this.popNandPushIfOK(1, this.millisecondClockValue());
>
> basically to reduce tedious boiler-plate code. Just in case you're
> wondering why some of the primitives appear to be missing ;)
>
> - Bert -
>
> PS: makeStArray: might prove useful
Thanks Bert,
I sent a git pull request for the new primitive, so hopefully you can
review it and include it in SqueakJS if it is not too horrible.
To my considerable surprise, DateAndTime>>now seems to be 70 times faster
on SqueakJS when using this primitive along with the enhancements in the
UTCDateAndTime repository.
I am inexperienced with JavaScript and Git, so in case any of this did not
get through, the proposed new function in Squeak.Primitives is:
primitiveUtcWithOffset: function(argCount) {
var d = new Date();
var posixMicroseconds = this.pos53BitIntFor(d.getTime() * 1000);
var offset = -60 * d.getTimezoneOffset();
if (argCount > 0) {
// either an Array or a DateAndTime in new UTC format with two ivars
var stWordIndexableObject = this.vm.stackValue(0);
stWordIndexableObject.pointers[0] = posixMicroseconds;
stWordIndexableObject.pointers[1] = offset;
this.popNandPushIfOK(argCount + 1, stWordIndexableObject);
return true;
}
var timeAndOffset = [
posixMicroseconds,
offset,
];
this.popNandPushIfOK(argCount + 1, this.makeStArray(timeAndOffset));
return true;
},
Dave
More information about the Vm-dev
mailing list