1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- Smalltalk createPackage: 'Platform-ImportExport'!
- Object subclass: #AbstractExporter
- instanceVariableNames: ''
- package: 'Platform-ImportExport'!
- !AbstractExporter commentStamp!
- I am an abstract exporter for Amber source code.
- ## API
- Use `#exportPackage:on:` to export a given package on a Stream.!
- !AbstractExporter methodsFor: 'accessing'!
- extensionMethodsOfPackage: aPackage
- | result |
-
- result := OrderedCollection new.
-
- (self extensionProtocolsOfPackage: aPackage) do: [ :each |
- result addAll: each ownMethods ].
-
- ^ result
- !
- extensionProtocolsOfPackage: aPackage
- | extensionName result |
-
- extensionName := '*', aPackage name.
- result := OrderedCollection new.
-
- "The classes must be loaded since it is extensions only.
- Therefore topological sorting (dependency resolution) does not matter here.
- Not sorting topologically improves the speed by a number of magnitude.
-
- Not to shuffle diffs, classes are sorted by their name."
-
- (Smalltalk classes asArray sorted: [ :a :b | a name < b name ]) do: [ :each |
- ({each. each theMetaClass} copyWithout: nil) do: [ :behavior |
- (behavior protocols includes: extensionName) ifTrue: [
- result add: (ExportMethodProtocol name: extensionName theClass: behavior) ] ] ].
- ^ result
- ! !
- !AbstractExporter methodsFor: 'output'!
- exportPackage: aPackage on: aStream
- self subclassResponsibility
- ! !
- AbstractExporter subclass: #ChunkExporter
- instanceVariableNames: ''
- package: 'Platform-ImportExport'!
- !ChunkExporter commentStamp!
- I am an exporter dedicated to outputting Amber source code in the classic Smalltalk chunk format.
- I do not output any compiled code.!
- !ChunkExporter methodsFor: 'accessing'!
- extensionCategoriesOfPackage: aPackage
- "Issue #143: sort protocol alphabetically"
- | name map result |
- name := aPackage name.
- result := OrderedCollection new.
- (Package sortedClasses: Smalltalk classes) do: [ :each |
- {each. each theMetaClass} do: [ :aClass |
- map := Dictionary new.
- aClass protocolsDo: [ :category :methods |
- category = ('*', name) ifTrue: [ map at: category put: methods ] ].
- result addAll: ((map keys sorted: [ :a :b | a <= b ]) collect: [ :category |
- MethodCategory name: category theClass: aClass methods: (map at: category) ]) ] ].
- ^ result
- !
- ownCategoriesOfClass: aClass
- "Answer the protocols of aClass that are not package extensions"
-
- "Issue #143: sort protocol alphabetically"
- | map |
- map := Dictionary new.
- aClass protocolsDo: [ :each :methods |
- (each match: '^\*') ifFalse: [ map at: each put: methods ] ].
- ^ (map keys sorted: [ :a :b | a <= b ]) collect: [ :each |
- MethodCategory name: each theClass: aClass methods: (map at: each) ]
- !
- ownCategoriesOfMetaClass: aClass
- "Issue #143: sort protocol alphabetically"
- ^ self ownCategoriesOfClass: aClass theMetaClass
- !
- ownMethodProtocolsOfClass: aClass
- "Answer a collection of ExportMethodProtocol object of aClass that are not package extensions"
-
- ^ aClass ownProtocols collect: [ :each |
- ExportMethodProtocol name: each theClass: aClass ]
- ! !
- !ChunkExporter methodsFor: 'convenience'!
- chunkEscape: aString
- "Replace all occurrences of !! with !!!! and trim at both ends."
- ^ (aString replace: '!!' with: '!!!!') trimBoth
- ! !
- !ChunkExporter methodsFor: 'output'!
- exportBehavior: aBehavior on: aStream
- aBehavior exportBehaviorDefinitionTo: aStream using: self.
- self
- exportProtocols: (self ownMethodProtocolsOfClass: aBehavior)
- on: aStream
- !
- exportCategoryEpilogueOf: aCategory on: aStream
- aStream write: ' !!'; lf; lf
- !
- exportCategoryPrologueOf: aCategory on: aStream
- aStream
- write: '!!';
- print: aCategory theClass;
- write: ' methodsFor: ';
- print: aCategory;
- write: '!!'
- !
- exportDefinitionOf: aClass on: aStream
- "Chunk format."
- aStream
- print: aClass superclass;
- write: ' subclass: ';
- printSymbol: aClass name;
- lf.
- "aClass traitComposition
- ifNotEmpty: [ aStream tab; write: {'uses: '. aClass traitCompositionDefinition}; lf ]."
- aStream
- tab;
- write: 'instanceVariableNames: ';
- print: (' ' join: aClass instanceVariableNames);
- lf;
- tab;
- write: 'package: ';
- print: aClass category;
- write: '!!';
- lf.
- aClass comment ifNotEmpty: [ aStream
- write: '!!'; print: aClass; write: ' commentStamp!!'; lf;
- write: { self chunkEscape: aClass comment. '!!' }; lf ].
- aStream lf
- !
- exportMetaDefinitionOf: aClass on: aStream
- | classIvars classTraitComposition |
- classIvars := aClass class instanceVariableNames.
- classTraitComposition := aClass class traitComposition.
- (classIvars notEmpty "or: [classTraitComposition notEmpty]") ifTrue: [
- aStream
- print: aClass theMetaClass.
- aStream space. "classTraitComposition
- ifEmpty: [ aStream space ]
- ifNotEmpty: [ aStream lf; tab; write: {'uses: '. aClass class traitCompositionDefinition}; lf; tab ]."
- aStream
- write: 'instanceVariableNames: ';
- print: (' ' join: classIvars);
- write: '!!'; lf; lf ]
- !
- exportMethod: aMethod on: aStream
- aStream
- lf; lf; write: (self chunkEscape: aMethod source); lf;
- write: '!!'
- !
- exportPackage: aPackage on: aStream
- self
- exportPackageDefinitionOf: aPackage on: aStream;
- exportPackageImportsOf: aPackage on: aStream.
-
- aPackage sortedClasses do: [ :each |
- self exportBehavior: each on: aStream.
- each theMetaClass ifNotNil: [ :meta | self exportBehavior: meta on: aStream ] ].
-
- self exportPackageTraitCompositionsOf: aPackage on: aStream.
- self
- exportProtocols: (self extensionProtocolsOfPackage: aPackage)
- on: aStream
- !
- exportPackageDefinitionOf: aPackage on: aStream
- aStream
- write: 'Smalltalk createPackage: ';
- print: aPackage name;
- write: '!!';
- lf
- !
- exportPackageImportsOf: aPackage on: aStream
- aPackage imports ifNotEmpty: [ :imports | aStream
- write: '(Smalltalk packageAt: ';
- print: aPackage name;
- write: ' ifAbsent: [ self error: ';
- print: 'Package not created: ', aPackage name;
- write: { ' ]) imports: '. self chunkEscape: aPackage importsDefinition. '!!' };
- lf ]
- !
- exportPackageTraitCompositionsOf: aPackage on: aStream
- aPackage traitCompositions ifNotEmpty: [ :traitCompositions |
- traitCompositions keysAndValuesDo: [ :key :value | self exportTraitComposition: value of: key on: aStream ].
- aStream write: '!! !!'; lf; lf ]
- !
- exportProtocol: aProtocol on: aStream
- aProtocol ownMethods ifNotEmpty: [ :methods |
- self exportProtocolPrologueOf: aProtocol on: aStream.
- methods do: [ :method |
- self exportMethod: method on: aStream ].
- self exportProtocolEpilogueOf: aProtocol on: aStream ]
- !
- exportProtocolEpilogueOf: aProtocol on: aStream
- aStream write: ' !!'; lf; lf
- !
- exportProtocolPrologueOf: aProtocol on: aStream
- aStream
- write: '!!';
- print: aProtocol theClass;
- write: ' methodsFor: ';
- print: aProtocol name;
- write: '!!'
- !
- exportProtocols: aCollection on: aStream
- aCollection do: [ :each |
- self exportProtocol: each on: aStream ]
- !
- exportTraitComposition: aTraitComposition of: aBehavior on: aStream
- aStream
- print: aBehavior;
- write: ' setTraitComposition: ';
- write: aBehavior traitCompositionDefinition;
- write: ' asTraitComposition!!';
- lf
- !
- exportTraitDefinitionOf: aClass on: aStream
- "Chunk format."
- aStream
- write: 'Trait named: '; printSymbol: aClass name; lf.
- "aClass traitComposition
- ifNotEmpty: [ aStream tab; write: {'uses: '. aClass traitCompositionDefinition}; lf ]."
- aStream
- tab; write: 'package: '; print: aClass category; write: '!!'; lf.
- aClass comment ifNotEmpty: [
- aStream
- write: '!!'; print: aClass; write: ' commentStamp!!'; lf;
- write: { self chunkEscape: aClass comment. '!!' }; lf ].
- aStream lf
- ! !
- AbstractExporter subclass: #Exporter
- instanceVariableNames: ''
- package: 'Platform-ImportExport'!
- !Exporter commentStamp!
- I am responsible for outputting Amber code into a JavaScript string.
- The generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.
- ## Use case
- I am typically used to save code outside of the Amber runtime (committing to disk, etc.).!
- !Exporter methodsFor: 'accessing'!
- ownMethodsOfClass: aClass
- "Issue #143: sort methods alphabetically"
- ^ ((aClass methodDictionary values) sorted: [ :a :b | a selector <= b selector ])
- reject: [ :each | (each protocol match: '^\*') ]
- !
- ownMethodsOfMetaClass: aClass
- "Issue #143: sort methods alphabetically"
- ^ self ownMethodsOfClass: aClass theMetaClass
- ! !
- !Exporter methodsFor: 'output'!
- exportBehavior: aBehavior on: aStream
- aBehavior exportBehaviorDefinitionTo: aStream using: self.
- aBehavior ownMethods do: [ :method |
- self exportMethod: method on: aStream ]
- !
- exportDefinitionOf: aClass on: aStream
- aStream
- lf;
- write: {
- '$core.addClass('.
- aClass name asJavaScriptSource. ', '.
- aClass superclass ifNil: [ 'null' ] ifNotNil: [ :superclass | superclass asJavaScriptSource ]. ', '.
- aClass instanceVariableNames asJavaScriptSource. ', '.
- aClass category asJavaScriptSource.
- ');' }.
- aClass comment ifNotEmpty: [
- aStream
- lf;
- write: '//>>excludeStart("ide", pragmas.excludeIdeData);'; lf;
- write: { aClass asJavaScriptSource. '.comment='. aClass comment crlfSanitized asJavaScriptSource. ';' }; lf;
- write: '//>>excludeEnd("ide");' ].
- aStream lf
- !
- exportMetaDefinitionOf: aClass on: aStream
- aStream lf.
- aClass theMetaClass instanceVariableNames ifNotEmpty: [ :classIvars | aStream
- write: { aClass theMetaClass asJavaScriptSource. '.iVarNames = '. classIvars asJavaScriptSource. ';' };
- lf ]
- !
- exportMethod: aMethod on: aStream
- aStream
- write: '$core.addMethod('; lf;
- write: '$core.method({'; lf;
- write: { 'selector: '. aMethod selector asJavaScriptSource. ',' }; lf;
- write: { 'protocol: '. aMethod protocol asJavaScriptSource. ',' }; lf;
- write: { 'fn: '. aMethod fn compiledSource. ',' }; lf;
- write: '//>>excludeStart("ide", pragmas.excludeIdeData);'; lf;
- write: { 'args: '. aMethod arguments asJavaScriptSource. ',' }; lf;
- write: { 'source: '. aMethod source asJavaScriptSource. ',' }; lf;
- write: { 'referencedClasses: '. aMethod referencedClasses asJavaScriptSource. ',' }; lf;
- write: '//>>excludeEnd("ide");'; lf;
- write: { 'messageSends: '. aMethod messageSends asJavaScriptSource }; lf;
- write: '}),'; lf;
- write: { aMethod methodClass asJavaScriptSource. ');' }; lf; lf
- !
- exportPackage: aPackage on: aStream
-
- self
- exportPackagePrologueOf: aPackage on: aStream;
- exportPackageDefinitionOf: aPackage on: aStream;
- exportPackageContextOf: aPackage on: aStream;
- exportPackageImportsOf: aPackage on: aStream;
- exportPackageTransportOf: aPackage on: aStream.
-
- aPackage sortedClasses do: [ :each |
- self exportBehavior: each on: aStream.
- each theMetaClass ifNotNil: [ :meta | self exportBehavior: meta on: aStream ] ].
-
- self exportPackageTraitCompositionsOf: aPackage on: aStream.
- (self extensionMethodsOfPackage: aPackage) do: [ :each |
- self exportMethod: each on: aStream ].
-
- self exportPackageEpilogueOf: aPackage on: aStream
- !
- exportPackageBodyBlockPrologueOf: aPackage on: aStream
- aStream
- write: 'if(!!("nilAsValue" in $boot))$boot.nilAsValue=$boot.nilAsReceiver;'; lf;
- write: 'var $core=$boot.api,nil=$boot.nilAsValue,$nil=$boot.nilAsReceiver,$recv=$boot.asReceiver,$globals=$boot.globals;'; lf
- !
- exportPackageContextOf: aPackage on: aStream
- aStream
- write: '$pkg.innerEval = function (expr) { return eval(expr); };';
- lf
- !
- exportPackageDefinitionOf: aPackage on: aStream
- aStream
- write: { 'var $pkg = $core.addPackage('. aPackage name asJavaScriptSource. ');' };
- lf
- !
- exportPackageEpilogueOf: aPackage on: aStream
- self subclassResponsibility
- !
- exportPackageImportsOf: aPackage on: aStream
- aPackage importsAsJson ifNotEmpty: [ :imports |
- aStream
- write: { '$pkg.imports = '. imports asJavaScriptSource. ';' };
- lf ]
- !
- exportPackagePrologueOf: aPackage on: aStream
- self subclassResponsibility
- !
- exportPackageTraitCompositionsOf: aPackage on: aStream
- aPackage traitCompositions ifNotEmpty: [ :traitCompositions |
- traitCompositions keysAndValuesDo: [ :key :value | self exportTraitComposition: value of: key on: aStream ].
- aStream lf ]
- !
- exportPackageTransportOf: aPackage on: aStream
- aStream
- write: { '$pkg.transport = '. aPackage transport asJSONString. ';' };
- lf
- !
- exportTraitComposition: aTraitComposition of: aBehavior on: aStream
- aStream write: {
- '$core.setTraitComposition('.
- aTraitComposition asJavaScriptSource.
- ', '.
- aBehavior asJavaScriptSource.
- ');' };
- lf
- !
- exportTraitDefinitionOf: aClass on: aStream
- aStream
- lf;
- write: {
- '$core.addTrait('.
- aClass name asJavaScriptSource. ', '.
- aClass category asJavaScriptSource.
- ');' }.
- aClass comment ifNotEmpty: [
- aStream
- lf;
- write: '//>>excludeStart("ide", pragmas.excludeIdeData);'; lf;
- write: { aClass asJavaScriptSource. '.comment='. aClass comment crlfSanitized asJavaScriptSource. ';' }; lf;
- write: '//>>excludeEnd("ide");' ].
- aStream lf
- ! !
- Exporter subclass: #AmdExporter
- instanceVariableNames: 'namespace'
- package: 'Platform-ImportExport'!
- !AmdExporter commentStamp!
- I am used to export Packages in an AMD (Asynchronous Module Definition) JavaScript format.!
- !AmdExporter methodsFor: 'output'!
- exportPackageEpilogueOf: aPackage on: aStream
- aStream
- write: '});';
- lf
- !
- exportPackagePrologueOf: aPackage on: aStream
- | importsForOutput loadDependencies pragmaStart pragmaEnd |
- pragmaStart := ''.
- pragmaEnd := ''.
- importsForOutput := self importsForOutput: aPackage.
- loadDependencies := self amdNamesOfPackages: aPackage loadDependencies.
- importsForOutput value ifNotEmpty: [
- pragmaStart := String lf, '//>>excludeStart("imports", pragmas.excludeImports);', String lf.
- pragmaEnd := String lf, '//>>excludeEnd("imports");', String lf ].
- aStream
- write: {
- 'define('.
- ((#('amber/boot' ':1:'), importsForOutput value, #(':2:'), loadDependencies asArray sorted) asJavaScriptSource
- replace: ',\s*["'']:1:["'']' with: pragmaStart)
- replace: ',\s*["'']:2:["'']' with: pragmaEnd.
- ', function('.
- ((((#('$boot' ':1:'), importsForOutput key, #(':2:')) join: ',')
- replace: ',\s*:1:' with: pragmaStart)
- replace: ',\s*:2:' with: pragmaEnd).
- '){"use strict";' };
- lf.
- self exportPackageBodyBlockPrologueOf: aPackage on: aStream
- ! !
- !AmdExporter methodsFor: 'private'!
- amdNamesOfPackages: anArray
- ^ (anArray
- select: [ :each | (self amdNamespaceOfPackage: each) notNil ])
- collect: [ :each | (self amdNamespaceOfPackage: each), '/', each name ]
- !
- amdNamespaceOfPackage: aPackage
- ^ (aPackage transport type = 'amd')
- ifTrue: [ aPackage transport namespace ]
- ifFalse: [ nil ]
- !
- importsForOutput: aPackage
- "Returns an association where key is list of import variables
- and value is list of external dependencies, with ones imported as variables
- put at the beginning with same order as is in key.
-
- For example imports:{'jQuery'->'jquery'. 'bootstrap'} would yield
- #('jQuery') -> #('jquery' 'bootstrap')"
- | namedImports anonImports importVarNames |
- namedImports := #().
- anonImports := #().
- importVarNames := #().
- aPackage imports do: [ :each | each isString
- ifTrue: [ anonImports add: each ]
- ifFalse: [ namedImports add: each value.
- importVarNames add: each key ]].
- ^ importVarNames -> (namedImports, anonImports)
- ! !
- Object subclass: #ChunkParser
- instanceVariableNames: 'stream last'
- package: 'Platform-ImportExport'!
- !ChunkParser commentStamp!
- I am responsible for parsing aStream contents in the chunk format.
- ## API
- ChunkParser new
- stream: aStream;
- nextChunk!
- !ChunkParser methodsFor: 'accessing'!
- last
- ^ last
- !
- stream: aStream
- stream := aStream
- ! !
- !ChunkParser methodsFor: 'reading'!
- nextChunk
- "The chunk format (Smalltalk Interchange Format or Fileout format)
- is a trivial format but can be a bit tricky to understand:
- - Uses the exclamation mark as delimiter of chunks.
- - Inside a chunk a normal exclamation mark must be doubled.
- - A non empty chunk must be a valid Smalltalk expression.
- - A chunk on top level with a preceding empty chunk is an instruction chunk:
- - The object created by the expression then takes over reading chunks.
- This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
- | char result chunk |
- result := '' writeStream.
- [ char := stream next.
- char notNil ] whileTrue: [
- char = '!!' ifTrue: [
- stream peek = '!!'
- ifTrue: [ stream next "skipping the escape double" ]
- ifFalse: [ ^ last := result contents trimBoth "chunk end marker found" ]].
- result nextPut: char ].
- ^ last := nil "a chunk needs to end with !!"
- ! !
- !ChunkParser class methodsFor: 'instance creation'!
- on: aStream
- ^ self new stream: aStream
- ! !
- Object subclass: #ClassCommentReader
- instanceVariableNames: 'class'
- package: 'Platform-ImportExport'!
- !ClassCommentReader commentStamp!
- I provide a mechanism for retrieving class comments stored on a file.
- See also `ClassCategoryReader`.!
- !ClassCommentReader methodsFor: 'accessing'!
- class: aClass
- class := aClass
- ! !
- !ClassCommentReader methodsFor: 'fileIn'!
- scanFrom: aChunkParser
- | chunk |
- chunk := aChunkParser nextChunk.
- chunk ifNotEmpty: [
- self setComment: chunk ].
- ! !
- !ClassCommentReader methodsFor: 'initialization'!
- initialize
- super initialize.
- ! !
- !ClassCommentReader methodsFor: 'private'!
- setComment: aString
- class comment: aString
- ! !
- Object subclass: #ClassProtocolReader
- instanceVariableNames: 'class category'
- package: 'Platform-ImportExport'!
- !ClassProtocolReader commentStamp!
- I provide a mechanism for retrieving class descriptions stored on a file in the Smalltalk chunk format.!
- !ClassProtocolReader methodsFor: 'accessing'!
- class: aClass category: aString
- class := aClass.
- category := aString
- ! !
- !ClassProtocolReader methodsFor: 'fileIn'!
- scanFrom: aChunkParser
- | chunk |
- [ chunk := aChunkParser nextChunk.
- chunk isEmpty ] whileFalse: [
- self compileMethod: chunk ]
- ! !
- !ClassProtocolReader methodsFor: 'initialization'!
- initialize
- super initialize.
- ! !
- !ClassProtocolReader methodsFor: 'private'!
- compileMethod: aString
- Compiler new install: aString forClass: class protocol: category
- ! !
- Object subclass: #ExportMethodProtocol
- instanceVariableNames: 'name theClass'
- package: 'Platform-ImportExport'!
- !ExportMethodProtocol commentStamp!
- I am an abstraction for a method protocol in a class / metaclass.
- I know of my class, name and methods.
- I am used when exporting a package.!
- !ExportMethodProtocol methodsFor: 'accessing'!
- methods
- ^ (self theClass methodsInProtocol: self name)
- sorted: [ :a :b | a selector <= b selector ]
- !
- name
- ^ name
- !
- name: aString
- name := aString
- !
- ownMethods
- ^ (self theClass ownMethodsInProtocol: self name)
- sorted: [ :a :b | a selector <= b selector ]
- !
- theClass
- ^ theClass
- !
- theClass: aClass
- theClass := aClass
- ! !
- !ExportMethodProtocol class methodsFor: 'instance creation'!
- name: aString theClass: aClass
- ^ self new
- name: aString;
- theClass: aClass;
- yourself
- ! !
- Object subclass: #Importer
- instanceVariableNames: 'lastSection lastChunk'
- package: 'Platform-ImportExport'!
- !Importer commentStamp!
- I can import Amber code from a string in the chunk format.
- ## API
- Importer new import: aString!
- !Importer methodsFor: 'accessing'!
- lastChunk
- ^ lastChunk
- !
- lastSection
- ^ lastSection
- ! !
- !Importer methodsFor: 'fileIn'!
- import: aStream
- | chunk result parser lastEmpty |
- parser := ChunkParser on: aStream.
- lastEmpty := false.
- lastSection := 'n/a, not started'.
- lastChunk := nil.
- [
- [ chunk := parser nextChunk.
- chunk isNil ] whileFalse: [
- chunk
- ifEmpty: [ lastEmpty := true ]
- ifNotEmpty: [
- lastSection := chunk.
- result := Compiler new evaluateExpression: chunk.
- lastEmpty
- ifTrue: [
- lastEmpty := false.
- result scanFrom: parser ]] ].
- lastSection := 'n/a, finished'
- ] on: Error do: [:e | lastChunk := parser last. e pass ].
- ! !
- Error subclass: #PackageCommitError
- instanceVariableNames: ''
- package: 'Platform-ImportExport'!
- !PackageCommitError commentStamp!
- I get signaled when an attempt to commit a package has failed.!
- Object subclass: #PackageHandler
- instanceVariableNames: ''
- package: 'Platform-ImportExport'!
- !PackageHandler commentStamp!
- I am responsible for handling package loading and committing.
- I should not be used directly. Instead, use the corresponding `Package` methods.!
- !PackageHandler methodsFor: 'accessing'!
- chunkContentsFor: aPackage
- ^ String streamContents: [ :str |
- self chunkExporter exportPackage: aPackage on: str ]
- !
- chunkExporterClass
- ^ ChunkExporter
- !
- commitPathJsFor: aPackage
- self subclassResponsibility
- !
- commitPathStFor: aPackage
- self subclassResponsibility
- !
- contentsFor: aPackage
- ^ String streamContents: [ :str |
- self exporter exportPackage: aPackage on: str ]
- !
- exporterClass
- self subclassResponsibility
- ! !
- !PackageHandler methodsFor: 'committing'!
- commit: aPackage
- self
- commit: aPackage
- onSuccess: []
- onError: [ :error |
- PackageCommitError new
- messageText: 'Commiting failed with reason: "' , (error responseText) , '"';
- signal ]
- !
- commit: aPackage onSuccess: aBlock onError: anotherBlock
- self
- commitJsFileFor: aPackage
- onSuccess: [
- self
- commitStFileFor: aPackage
- onSuccess: [ aPackage beClean. aBlock value ]
- onError: anotherBlock ]
- onError: anotherBlock
- !
- commitJsFileFor: aPackage onSuccess: aBlock onError: anotherBlock
- self
- ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
- data: (self contentsFor: aPackage)
- onSuccess: aBlock
- onError: anotherBlock
- !
- commitStFileFor: aPackage onSuccess: aBlock onError: anotherBlock
- self
- ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
- data: (self chunkContentsFor: aPackage)
- onSuccess: aBlock
- onError: anotherBlock
- ! !
- !PackageHandler methodsFor: 'error handling'!
- onCommitError: anError
- PackageCommitError new
- messageText: 'Commiting failed with reason: "' , (anError responseText) , '"';
- signal
- ! !
- !PackageHandler methodsFor: 'factory'!
- chunkExporter
- ^ self chunkExporterClass new
- !
- exporter
- ^ self exporterClass new
- ! !
- !PackageHandler methodsFor: 'loading'!
- load: aPackage
- "Should return a TThenable"
- self subclassResponsibility
- ! !
- !PackageHandler methodsFor: 'private'!
- ajaxPutAt: aURL data: aString onSuccess: aBlock onError: anotherBlock
- | xhr |
- xhr := Platform newXhr.
- xhr open: 'PUT' url: aURL async: true.
- xhr onreadystatechange: [
- xhr readyState = 4 ifTrue: [
- (xhr status >= 200 and: [ xhr status < 300 ])
- ifTrue: aBlock
- ifFalse: anotherBlock ]].
- xhr send: aString
- ! !
- PackageHandler subclass: #AmdPackageHandler
- instanceVariableNames: ''
- package: 'Platform-ImportExport'!
- !AmdPackageHandler commentStamp!
- I am responsible for handling package loading and committing.
- I should not be used directly. Instead, use the corresponding `Package` methods.!
- !AmdPackageHandler methodsFor: 'accessing'!
- commitPathJsFor: aPackage
- ^ self toUrl: (self namespaceFor: aPackage)
- !
- commitPathStFor: aPackage
- "If _source is not mapped, .st will be committed to .js path.
- It is recommended not to use _source as it can be deprecated."
-
- | path pathWithout |
- path := self toUrl: (self namespaceFor: aPackage), '/_source'.
- pathWithout := self commitPathJsFor: aPackage.
- ^ path = (pathWithout, '/_source') ifTrue: [ pathWithout ] ifFalse: [ path ]
- !
- exporterClass
- ^ AmdExporter
- ! !
- !AmdPackageHandler methodsFor: 'committing'!
- namespaceFor: aPackage
- ^ aPackage transport namespace
- ! !
- !AmdPackageHandler methodsFor: 'loading'!
- load: aPackage
- ^ Promise new: [ :model |
- Smalltalk amdRequire
- ifNil: [ self error: 'AMD loader not present' ]
- ifNotNil: [ :require |
- require
- value: { (self namespaceFor: aPackage), '/', aPackage name }
- value: [ :result | model value: result ]
- value: [ :error | model signal: error ] ] ]
- ! !
- !AmdPackageHandler methodsFor: 'private'!
- toUrl: aString
- ^ Smalltalk amdRequire
- ifNil: [ self error: 'AMD loader not present' ]
- ifNotNil: [ :require | require provided toUrl: aString ]
- ! !
- !AmdPackageHandler class methodsFor: 'commit paths'!
- defaultNamespace
- ^ Smalltalk defaultAmdNamespace
- !
- defaultNamespace: aString
- Smalltalk defaultAmdNamespace: aString
- ! !
- Object subclass: #PackageTransport
- instanceVariableNames: 'package'
- package: 'Platform-ImportExport'!
- !PackageTransport commentStamp!
- I represent the transport mechanism used to commit a package.
- My concrete subclasses have a `#handler` to which committing is delegated.!
- !PackageTransport methodsFor: 'accessing'!
- commitHandlerClass
- self subclassResponsibility
- !
- definition
- ^ ''
- !
- package
- ^ package
- !
- package: aPackage
- package := aPackage
- !
- type
- ^ self class type
- ! !
- !PackageTransport methodsFor: 'committing'!
- commit
- self commitHandler commit: self package
- !
- commitOnSuccess: aBlock onError: anotherBlock
- self commitHandler
- commit: self package
- onSuccess: aBlock
- onError: anotherBlock
- ! !
- !PackageTransport methodsFor: 'converting'!
- asJavaScriptObject
- ^ #{ 'type' -> self type }
- ! !
- !PackageTransport methodsFor: 'factory'!
- commitHandler
- ^ self commitHandlerClass new
- ! !
- !PackageTransport methodsFor: 'initialization'!
- setupFromJson: anObject
- "no op. override if needed in subclasses"
- ! !
- !PackageTransport methodsFor: 'loading'!
- load
- ^ (self commitHandler load: self package)
- then: [ Smalltalk postLoad ]
- ! !
- PackageTransport class instanceVariableNames: 'registry'!
- !PackageTransport class methodsFor: 'accessing'!
- classRegisteredFor: aString
- ^ registry at: aString
- !
- defaultType
- ^ AmdPackageTransport type
- !
- type
- "Override in subclasses"
- ^ nil
- ! !
- !PackageTransport class methodsFor: 'initialization'!
- initialize
- super initialize.
- self == PackageTransport
- ifTrue: [ registry := #{} ]
- ifFalse: [ self register ]
- ! !
- !PackageTransport class methodsFor: 'instance creation'!
- for: aString
- ^ (self classRegisteredFor: aString) new
- !
- fromJson: anObject
- anObject ifNil: [ ^ self for: self defaultType ].
-
- ^ (self for: anObject type)
- setupFromJson: anObject;
- yourself
- ! !
- !PackageTransport class methodsFor: 'registration'!
- register
- PackageTransport register: self
- !
- register: aClass
- aClass type ifNotNil: [
- registry at: aClass type put: aClass ]
- ! !
- PackageTransport subclass: #AmdPackageTransport
- instanceVariableNames: 'namespace'
- package: 'Platform-ImportExport'!
- !AmdPackageTransport commentStamp!
- I am the default transport for committing packages.
- See `AmdExporter` and `AmdPackageHandler`.!
- !AmdPackageTransport methodsFor: 'accessing'!
- commitHandlerClass
- ^ AmdPackageHandler
- !
- definition
- ^ String streamContents: [ :stream | stream
- write: { self class name. ' namespace: ' }; print: self namespace ]
- !
- namespace
- ^ namespace ifNil: [ self defaultNamespace ]
- !
- namespace: aString
- namespace := aString
- ! !
- !AmdPackageTransport methodsFor: 'actions'!
- setPath: aString
- "Set the path the the receiver's `namespace`"
-
- require provided config: #{
- 'paths' -> #{
- self namespace -> aString
- }
- }.
- ! !
- !AmdPackageTransport methodsFor: 'converting'!
- asJavaScriptObject
- ^ super asJavaScriptObject
- at: 'amdNamespace' put: self namespace;
- yourself
- ! !
- !AmdPackageTransport methodsFor: 'defaults'!
- defaultNamespace
- ^ Smalltalk defaultAmdNamespace
- ! !
- !AmdPackageTransport methodsFor: 'initialization'!
- setupFromJson: anObject
- self namespace: (anObject at: 'amdNamespace')
- ! !
- !AmdPackageTransport methodsFor: 'printing'!
- printOn: aStream
- super printOn: aStream.
- aStream
- nextPutAll: ' (AMD Namespace: ';
- nextPutAll: self namespace;
- nextPutAll: ')'
- ! !
- !AmdPackageTransport class methodsFor: 'accessing'!
- type
- ^ 'amd'
- ! !
- !AmdPackageTransport class methodsFor: 'instance creation'!
- namespace: aString
- ^ self new
- namespace: aString;
- yourself
- ! !
- !Class methodsFor: '*Platform-ImportExport'!
- exportBehaviorDefinitionTo: aStream using: anExporter
- anExporter exportDefinitionOf: self on: aStream
- ! !
- !Metaclass methodsFor: '*Platform-ImportExport'!
- exportBehaviorDefinitionTo: aStream using: anExporter
- anExporter exportMetaDefinitionOf: self instanceClass on: aStream
- ! !
- !Package methodsFor: '*Platform-ImportExport'!
- commit
- ^ self transport commit
- !
- load
- ^ self transport load
- !
- loadFromNamespace: aString
- ^ self transport
- namespace: aString;
- load
- ! !
- !Package class methodsFor: '*Platform-ImportExport'!
- load: aPackageName
- ^ (self named: aPackageName) load
- !
- load: aPackageName fromNamespace: aString
- ^ (self named: aPackageName) loadFromNamespace: aString
- ! !
- !TBehaviorProvider methodsFor: '*Platform-ImportExport'!
- methodsFor: aString
- ^ ClassProtocolReader new
- class: self category: aString;
- yourself
- !
- methodsFor: aString stamp: aStamp
- "Added for file-in compatibility, ignores stamp."
- ^ self methodsFor: aString
- ! !
- !TMasterBehavior methodsFor: '*Platform-ImportExport'!
- commentStamp
- ^ ClassCommentReader new
- class: self;
- yourself
- !
- commentStamp: aStamp prior: prior
- ^ self commentStamp
- ! !
- !Trait methodsFor: '*Platform-ImportExport'!
- exportBehaviorDefinitionTo: aStream using: anExporter
- anExporter exportTraitDefinitionOf: self on: aStream
- ! !
|