(make-package 'kin-lang-assembler) (use-package 'kin-lang-assembler)This defines the assembler package and tells the interpreter to use it as part of the search path for classes
(use-class 'kin-lang-assembler:jvm-type) (use-class 'kin-lang-assembler:access-flags) (use-class 'kin-lang-assembler:code-block)This tells the interpreter to use the public static fields and methods fo those classes.
(setf cc (make-instance 'compiled-class)) (setf (access-flags cc) (logior *acc-super* *acc-public*)) (setf (this-class cc) (build-class (constant-pool cc) "kin.user.Foo")) (setf (super-class cc) (build-class (constant-pool cc) "java.lang.Object"))Creates an instance of compiled-class, which holds the bytecode structure for a Java class. The variable 'cc' holds a reference to the class, and the name of the compiled class and the super class are set.
(add-method cc (setf init (make-instance 'method-info cc "<init>" "()V"))) (setf (access-flags init) *acc-public*) (setf code (create-code-block init)) (setf (instructions code) (let ((self (variable code *reference* 0))) (list (make-load code self) (make-invokespecial code (build-methodref (constant-pool cc) "java.lang.Object" "<init>" "()V")) (make-return code))))Adds and initialization method for objects of the class. This is the default init method, which calls the super class init method only.
(add-method cc (setf foo (make-instance 'method-info cc "foo" "()I"))) (setf (access-flags foo) (logior *acc-public* *acc-static*)) (setf code (create-code-block foo)) (setf (instructions code) (list (make-iconst code 7) (make-iconst code 6) (make-mul code *integer*) (make-return code *integer*)))Adds a static method 'foo' which multiplies the ints 6 and 7.
(setf data (bytes cc))Sets the variable 'data' to the serialized bytes of the compiled class.
(setf cl (class-loader (class 'foo)))Finds the class loader used to load the core of kin, by finding the class loader for the class of the symbol 'foo.
The return from this method will be a KinClassLoader, which allows dynamic loading of classes.
(define-dynamic-class cl "kin.user.Foo" data)Calls the define-dynamic-class method in the kin class loader to use the binary data to define the class 'kin.user.Foo'.
(use-class 'kin.user:Foo)Uses the symbols exported by the class 'kin.user.Foo'. Currently, this has to be the Java name no the kin name of the class.
(foo)Calls the 'foo' function, which is the static method 'foo' imported from the class 'kin.user.Foo', which we assembled earlier.
This part of kin is still very much under development, for example (use-class 'kin-user:foo) doesn't work with the above dynamic class but would if the class was in the static classpath.
It is intended that these mechanisms form the basis of kin's compiler.
Pete Kirkham
2003-08-20.