Primitive Methods

January 14, 2011

The core of Silver Smalltalk consists of a Smalltalk code compiler and a bytecode interpreter. This interpreter (STVM.xxx) has now been implemented in versions for .Net, Java, JavaScript, and Flash.

The interpreter implements Squeak bytecodes internally and accesses resources from the host environment through “Primitive Methods”.

“Primitive Methods” are used like any other Smalltalk methods but, instead of the method being executed within the interpreter, a call is made to the host environment.

There are two separate syntaxes for Primive Methods. The first is resolved at compile time and has the following syntax:

%ClassName:MethodName:Arg0ClassName:Arg1ClassName:....%

Here is an example primitive method in Object->System->Char:
isUpper: aChar
%System.Char:IsUpper:System.Char%

This is a call to the .Net library function “Boolean System.Char.IsUpper(System.Char ch)”. In Smalltalk, it is called like any other method. For example,

Char isUpper: $a ==> false
Char isUpper: $A ==> true

Of course, the exact primitive method syntax will depend upon the host environment.

There are also “dynamic primitives” which are mixed directly in Smalltalk code and don’t require creating new methods.

They have a syntax that begins with “@”:
“@MethodName:with:Arg0ClassName…”

Here is an example using two dynamic primitives:
('hello world' @Substring: 3 with: 6) @ToUpper
==> ‘LO WOR’

Dynamic primitives are more convenient; compiled primitives can be more efficient and makes it easier to write code that runs in multiple environments.

In summary, the SST interpreter can access virtually any resource that is available in the host environment libraries.

Advertisements

4 Responses to “Primitive Methods”

  1. Chip Nowacek Says:

    Does your

    (‘hello world’ @Substring: 3 with: 6) @ToUpper
    ==> ‘LO WOR’

    example mean indices in SST are zero (0) based?

  2. Chip Nowacek Says:

    I think I’d rather add methods to the appropriate classes and not have to think about it.

  3. Chip Nowacek Says:

    Should I use .Net documentation to understand Primitive Methods in Silverlight?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: