MyScript.st 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. Object subclass: #MyScript
  2. instanceVariableNames: ''
  3. package: 'MyScript'!
  4. !MyScript class methodsFor: 'main'!
  5. main
  6. "Just a trivial example showing some meta programming by
  7. creating a class, compiling some methods and then exporting
  8. this package in javascript format to stdout"
  9. | myClass method |
  10. console log: 'Creating new class #Dummy'.
  11. Object subclass: #Dummy instanceVariableNames: '' package: 'Dummy'.
  12. myClass := Smalltalk globals at: #Dummy.
  13. console log: 'Add method #foo to class #Dummy'.
  14. myClass compile: 'foo ^10' protocol: 'foo'.
  15. console log: 'Add method #bar to class #Dummy'.
  16. myClass compile: 'bar ^ self foo * 2' protocol: 'foo'.
  17. console log: 'Add amd transport to package #Dummy'.
  18. (Smalltalk packageAt: 'Dummy') transport: (AmdPackageTransport namespace: 'dummy').
  19. console log: '--- Exported JavaScript for class #Dummy ---'.
  20. console log: (String streamContents: [ :str | AmdExporter new exportPackage: (Smalltalk packageAt: 'Dummy') on: str ] ).
  21. console log: '--- Exported JavaScript for class #Dummy End---'.
  22. ! !