src/JavaVM.st
branchjk_new_structure
changeset 1026 5badd1d31864
parent 1024 e00483588b3d
child 1027 43dc608e14aa
equal deleted inserted replaced
1025:2c9c84bdd863 1026:5badd1d31864
  1803      Following >>make it working, make it fast<< rule"
  1803      Following >>make it working, make it fast<< rule"
  1804     
  1804     
  1805     ObjectMemory
  1805     ObjectMemory
  1806         javaJustInTimeCompilation: false;
  1806         javaJustInTimeCompilation: false;
  1807         javaNativeCodeOptimization: false.
  1807         javaNativeCodeOptimization: false.
  1808 
  1808      "Java lot of stack. Change default to 2 MB"
  1809     "Java lot of stack. Change default to 2 MB"
  1809     Process defaultMaximumStackSize: 1024 * 1024 * 2.
  1810     Process defaultMaximumStackSize:1024 * 1024 * 2.
       
  1811 
       
  1812     JavaObject autoload.
  1810     JavaObject autoload.
  1813     JavaContext autoload.
  1811     JavaContext autoload.
  1814     MonitorTrace ifNil: [ self initialize ].
  1812     MonitorTrace ifNil: [ self initialize ].
  1815     self initializeAdditionalJavaProtocol.
  1813     self initializeAdditionalJavaProtocol.
  1816     self releaseAllJavaResources.
  1814     self releaseAllJavaResources.
  1817     JavaConsoleStream := Transcript.
  1815     JavaConsoleStream := Transcript.
  1818     Java initAllStaticFields.
  1816     Java initAllStaticFields.
  1819     Java markAllClassesUninitialized.
  1817     Java markAllClassesUninitialized.
  1820 
       
  1821     ZipCache := OrderedCollection new.
  1818     ZipCache := OrderedCollection new.
  1822     ZipEntryCache := OrderedCollection new.
  1819     ZipEntryCache := OrderedCollection new.
  1823     ZipInflaters := OrderedCollection new.
  1820     ZipInflaters := OrderedCollection new.
  1824     
  1821     
  1825     "/ force re-resolving;
  1822     "/ force re-resolving;
  1826     "/ otherwise, class-inits would not be called
  1823     "/ otherwise, class-inits would not be called
  1827     "/ by resolver ... (sigh)
  1824     "/ by resolver ... (sigh)
  1828     
  1825     
  1829     JavaNativeMethod flushAllCachedNativeMethods.
  1826     JavaNativeMethod flushAllCachedNativeMethods.
  1830     JavaMethod allSubInstancesDo: 
  1827     JavaMethod 
  1831             [:m | 
  1828         allSubInstancesDo: [
       
  1829             :m | 
  1832             m checked: false.
  1830             m checked: false.
  1833             m code: nil. ].
  1831             m code: nil.
       
  1832         ].
  1834     ObjectMemory flushCaches.
  1833     ObjectMemory flushCaches.
  1835     
  1834     
  1836     "/ sigh - must flush inline caches.
  1835     "/ sigh - must flush inline caches.
  1837     
  1836     
  1838     JavaClass startRememberingOrderOfClassInits.
  1837     JavaClass startRememberingOrderOfClassInits.
  1856     ObjectMemory addDependent: self.
  1855     ObjectMemory addDependent: self.
  1857 
  1856 
  1858     "
  1857     "
  1859      JavaVM initialize.
  1858      JavaVM initialize.
  1860      JavaVM initializeVM."
  1859      JavaVM initializeVM."
  1861 
       
  1862     "Created: / 03-01-1998 / 21:29:09 / cg"
  1860     "Created: / 03-01-1998 / 21:29:09 / cg"
  1863     "Modified: / 14-12-1999 / 18:58:56 / cg"
  1861     "Modified: / 14-12-1999 / 18:58:56 / cg"
  1864     "Modified: / 15-10-2010 / 15:27:45 / Jan Kurs <kurs.jan@post.cz>"
  1862     "Modified: / 15-10-2010 / 15:27:45 / Jan Kurs <kurs.jan@post.cz>"
  1865     "Modified: / 01-04-2011 / 15:33:39 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
  1863     "Modified: / 01-04-2011 / 15:33:39 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
  1866     "Modified: / 14-09-2011 / 22:06:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1864     "Modified: / 14-09-2011 / 22:06:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2591     "Created: / 22-11-2010 / 16:31:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2589     "Created: / 22-11-2010 / 16:31:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2592     "Modified: / 09-02-2011 / 00:47:49 / Marcel Hlopko <hlopik@gmail.com>"
  2590     "Modified: / 09-02-2011 / 00:47:49 / Marcel Hlopko <hlopik@gmail.com>"
  2593 !
  2591 !
  2594 
  2592 
  2595 javaStringObjectForString:string interned:intern 
  2593 javaStringObjectForString:string interned:intern 
  2596     self breakPoint:#mh info:'refactor my sender to use reflection directly'.
  2594     
  2597     ^self reflection javaStringObjectForString:string interned:intern.
  2595     ^self reflection javaStringObjectForString:string interned:intern.
  2598 
  2596 
  2599     "Created: / 22-11-2010 / 16:28:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2597     "Created: / 22-11-2010 / 16:28:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2600     "Modified: / 22-11-2010 / 17:57:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2598     "Modified: / 22-11-2010 / 17:57:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  2601     "Modified: / 09-02-2011 / 01:04:21 / Marcel Hlopko <hlopik@gmail.com>"
  2599     "Modified: / 09-02-2011 / 01:04:21 / Marcel Hlopko <hlopik@gmail.com>"
  4810 
  4808 
  4811     "Modified: / 06-02-1998 / 03:12:17 / cg"
  4809     "Modified: / 06-02-1998 / 03:12:17 / cg"
  4812     "Created: / 10-12-2010 / 15:11:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4810     "Created: / 10-12-2010 / 15:11:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4813 !
  4811 !
  4814 
  4812 
  4815 _java_lang_ClassLoader_defineClass1: nativeContext
  4813 _java_lang_ClassLoader_defineClass1: nativeContext 
  4816 
       
  4817     <javanative: 'java/lang/ClassLoader' name: 'defineClass1'>
  4814     <javanative: 'java/lang/ClassLoader' name: 'defineClass1'>
  4818     "
  4815     "
  4819     private native Class defineClass1(String name, byte[] b, int off, int len,
  4816      private native Class defineClass1(String name, byte[] b, int off, int len,
  4820                                       ProtectionDomain pd, String source);
  4817                                       ProtectionDomain pd, String source);"
  4821     "
  4818     
  4822     | name b off len pd source bs cls |
  4819     | className  b  off  len  pd  source  bs  cls |
  4823     name :=  Java as_ST_String: (nativeContext argAt:1).
  4820 self breakPoint:#mh.
  4824     b := nativeContext argAt:2.
  4821     className := Java as_ST_String: (nativeContext argAt: 1).
  4825     off := nativeContext argAt:3.
  4822     b := nativeContext argAt: 2.
  4826     len := nativeContext argAt:4.
  4823     off := nativeContext argAt: 3.
  4827     pd := nativeContext argAt:5.
  4824     len := nativeContext argAt: 4.
  4828     source := Java as_ST_String: (nativeContext argAt:6).
  4825     pd := nativeContext argAt: 5.
  4829 
  4826     source := Java as_ST_String: (nativeContext argAt: 6).
  4830     bs := (off = 0 and: [len = b size]) 
  4827     bs := (off = 0 and: [ len = b size ]) ifTrue: [ b readStream ] ifFalse: [
  4831             ifTrue:[b readStream]
  4828                 (b copyFrom: off + 1 to: off + len) readStream
  4832             ifFalse:[(b copyFrom: off + 1 to: off + len) readStream].
  4829             ].
  4833     [
  4830     [ cls := Java javaRegistry loadStream: bs loader: nativeContext receiver. ] 
  4834         cls := JavaClassReader readStream: bs loader: nativeContext receiver.
  4831         on: JavaClassReader invalidClassFormatSignal
  4835     ] on: JavaClassReader invalidClassFormatSignal do:[
  4832         do: [
  4836         self throwClassFormatError.
  4833     self throwClassFormatError.
  4837         ^self.
  4834     ^ self.
  4838     ].
  4835 ].
  4839     cls classLoader: nativeContext receiver.
  4836     cls classLoader: nativeContext receiver.
  4840     "FIXME: What to do with source?"
  4837      "FIXME: What to do with source?"
  4841 
  4838     ^ self reflection javaClassObjectForClass: cls.
  4842     ^self reflection javaClassObjectForClass: cls.
       
  4843 
  4839 
  4844     "Modified: / 31-08-2011 / 21:18:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4840     "Modified: / 31-08-2011 / 21:18:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4845 !
  4841 !
  4846 
  4842 
  4847 _java_lang_ClassLoader_findBootstrapClass: nativeContext 
  4843 _java_lang_ClassLoader_findBootstrapClass: nativeContext 
  4848     <javanative: 'java/lang/ClassLoader' name: 'findBootstrapClass'>
  4844     <javanative: 'java/lang/ClassLoader' name: 'findBootstrapClass'>
  4849     | nm  class |
  4845     | nm  class |
  4850 
  4846 self breakPoint:#mh.
  4851     nm := Java as_ST_String: (nativeContext argAt: 1).
  4847     nm := Java as_ST_String: (nativeContext argAt: 1).
  4852     class := JavaClassReader 
  4848     class := Java javaRegistry classForName: nm definedBy: nil.
  4853                 loadClassLazy: nm
       
  4854                 classpath: Java release classPath
       
  4855                 ignoring: Set new.
       
  4856     ^ class notNil 
  4849     ^ class notNil 
  4857         ifTrue: [ self reflection javaClassObjectForClass: class ]
  4850         ifTrue: [ self reflection javaClassObjectForClass: class ]
  4858         ifFalse: [ self throwClassNotFoundException: nm ]
  4851         ifFalse: [ self throwClassNotFoundException: nm ].
  4859 
  4852 
  4860     "Modified: / 18-10-2011 / 23:13:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4853     "Modified: / 08-09-2011 / 08:04:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4861 !
  4854 !
  4862 
  4855 
  4863 _java_lang_ClassLoader_findLoadedClass0: nativeContext
  4856 _java_lang_ClassLoader_findLoadedClass0: nativeContext
  4864 
  4857 
  4865     <javanative: 'java/lang/ClassLoader' name: 'findLoadedClass0'>
  4858     <javanative: 'java/lang/ClassLoader' name: 'findLoadedClass0'>
  4886     "Nothing to do"
  4879     "Nothing to do"
  4887 
  4880 
  4888     "Created: / 09-11-2010 / 20:55:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4881     "Created: / 09-11-2010 / 20:55:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4889 !
  4882 !
  4890 
  4883 
  4891 _java_lang_ClassLoader_resolveClass0: nativeContext
  4884 _java_lang_ClassLoader_resolveClass0: nativeContext 
  4892 
       
  4893     <javanative: 'java/lang/ClassLoader' name: 'resolveClass0'>
  4885     <javanative: 'java/lang/ClassLoader' name: 'resolveClass0'>
  4894 
       
  4895     "resolve a new class as previously created by defineClass0"
  4886     "resolve a new class as previously created by defineClass0"
  4896     
  4887     
  4897     |jClassLoader jCls cls anyUnresolved|
  4888     | jClassLoader  jCls  cls  anyUnresolved |
  4898 
  4889 
       
  4890     self breakPoint: #mh.
  4899     jClassLoader := nativeContext receiver.
  4891     jClassLoader := nativeContext receiver.
  4900     jCls := nativeContext argAt:1.
  4892     jCls := nativeContext argAt: 1.
  4901     jCls isNil ifTrue:[
  4893     jCls isNil ifTrue: [
  4902         self halt.
  4894         self halt.
  4903         ^ nil
  4895         ^ nil
  4904     ].
  4896     ].
  4905     cls := self reflection classForJavaClassObject:jCls.
  4897     cls := self reflection classForJavaClassObject: jCls.
  4906     cls isNil ifTrue:[
  4898     cls isNil ifTrue: [
  4907         self halt.
  4899         self halt.
  4908         ^ nil
  4900         ^ nil
  4909     ].
  4901     ].
  4910     JavaClassReader classLoaderQuerySignal answer:jClassLoader
  4902     JavaClassReader classLoaderQuerySignal answer: jClassLoader
  4911         do:[
  4903         do: [self breakPoint:#mh. 
  4912             JavaClassReader resolveClass:cls.
  4904         "correct this so it works with new resolving"
       
  4905             JavaClassReader resolveClass: cls.
  4913             
  4906             
  4914 "/        JavaClassReader postLoadActions:true.
  4907 "/        JavaClassReader postLoadActions:true.
  4915             
  4908             
  4916             anyUnresolved := false.
  4909             anyUnresolved := false.
  4917             cls constantPool do:[:entry | 
  4910             cls constantPool do: [
  4918                 (entry isMemberOf:JavaUnresolvedClassConstant) ifTrue:[
  4911                 :entry | 
  4919                     self halt:'debugHalt'.
  4912                 (entry isMemberOf: JavaUnresolvedClassConstant) ifTrue: [
       
  4913                     self halt: 'debugHalt'.
  4920                     entry preResolve.
  4914                     entry preResolve.
  4921                     self halt:'debugHalt'.
  4915                     self halt: 'debugHalt'.
  4922                     anyUnresolved := true.
  4916                     anyUnresolved := true.
  4923                 ]
  4917                 ]
  4924             ]
  4918             ]
  4925         ].
  4919         ].
  4926     anyUnresolved ifTrue:[
  4920     anyUnresolved ifTrue: [
  4927         jClassLoader notNil ifTrue:[
  4921         jClassLoader notNil ifTrue: [
  4928             "/ any unresolved left -> try resolving with standard loader
  4922             "/ any unresolved left -> try resolving with standard loader
  4929             JavaClassReader classLoaderQuerySignal answer:nil
  4923             JavaClassReader classLoaderQuerySignal answer: nil
  4930                 do:[
  4924                 do: [
  4931                     JavaClassReader postLoadActions:true.
  4925                     cls constantPool do: [
  4932                     cls constantPool do:[:entry | 
  4926                         :entry | 
  4933                         (entry isMemberOf:JavaUnresolvedClassConstant) ifTrue:[
  4927                         (entry isMemberOf: JavaUnresolvedClassConstant) ifTrue: [
  4934                             self halt:'debugHalt'.
  4928                             self halt: 'debugHalt'.
  4935                             entry preResolve.
  4929                             entry preResolve.
  4936                             self halt:'debugHalt'.
  4930                             self halt: 'debugHalt'.
  4937                         ]
  4931                         ]
  4938                     ]
  4932                     ]
  4939                 ]
  4933                 ]
  4940         ].
  4934         ].
  4941     ].
  4935     ].
  7273 
  7267 
  7274     "Modified: / 27.1.2000 / 02:36:01 / cg"
  7268     "Modified: / 27.1.2000 / 02:36:01 / cg"
  7275     "Created: / 27.1.2000 / 02:56:37 / cg"
  7269     "Created: / 27.1.2000 / 02:56:37 / cg"
  7276 !
  7270 !
  7277 
  7271 
  7278 _ClassLoader_defineClass0:nativeContext
  7272 _ClassLoader_defineClass0: nativeContext 
  7279     "create a new class from a given byteArray.
  7273     "create a new class from a given byteArray.
  7280      Here, construct a stream on it and pass the work to the
  7274      Here, construct a stream on it and pass the work to the
  7281      JavaClassReader."
  7275      JavaClassReader."
  7282 
  7276     
  7283     |jClassLoader jName name data offset length inStream newClass
  7277     | jClassLoader  jName  data  offset  length  inStream  newClass  jClass |
  7284      loaderStub jClass|
       
  7285 
  7278 
  7286     jClassLoader := nativeContext receiver.
  7279     jClassLoader := nativeContext receiver.
  7287     jName := nativeContext argAt:1.
  7280     jName := nativeContext argAt: 1.
  7288 
  7281     data := nativeContext argAt: 2.
  7289     "/ className is now optional ...
  7282     offset := nativeContext argAt: 3.
  7290 "/    jName isNil ifTrue:[
  7283     length := nativeContext argAt: 4.
  7291 "/        self internalError:'nil name in defineClass'.
       
  7292 "/        ^ nil
       
  7293 "/    ] ifFalse:[
       
  7294 "/        name := Java as_ST_String:jName.
       
  7295 "/    ].
       
  7296 
       
  7297     data := nativeContext argAt:2.
       
  7298     offset := nativeContext argAt:3.
       
  7299     length := nativeContext argAt:4.
       
  7300 
       
  7301     inStream := data readStream.
  7284     inStream := data readStream.
  7302     inStream position:offset + 1.
  7285     inStream position: offset + 1.
  7303     inStream readLimit:(offset + length).
  7286     inStream readLimit: (offset + length).
  7304 
  7287     newClass := JavaClassRegistry loadStream: inStream loader: jClassLoader.
  7305 "/    loaderStub := Plug new.
  7288     newClass isNil ifTrue: [
  7306 "/    loaderStub respondTo:#loadClass: with:[:clsName |
  7289         Logger 
  7307 "/                                                |jName|
  7290             log: '_ClassLoader_defineClass0: was not able to read given class'
  7308 "/
  7291             severity: #severe
  7309 "/self halt.
  7292             facility: #JavaVM.
  7310 "/                                                jName := Java as_String:clsName.
  7293         self breakPoint: #mh.
  7311 "/                                                "/ jClassLoader loadClass:jName
       
  7312 "/                                                jClassLoader 
       
  7313 "/                                                    perform:#'loadClass(Ljava/lang/String;)Ljava/lang/Class;'
       
  7314 "/                                                    with:jName.
       
  7315 "/                                          ].
       
  7316 
       
  7317 "/    ('JAVA [info]: defining class ...') infoPrintCR.
       
  7318 
       
  7319 "/    self internalError:'break'.
       
  7320 
       
  7321     newClass := JavaClassReader 
       
  7322                     readStream:inStream 
       
  7323                     loader:jClassLoader "loaderStub"
       
  7324                     loadUnresolved:false.
       
  7325 
       
  7326     newClass isNil ifTrue:[
       
  7327         ('JAVA [info]: defineClass failed') infoPrintCR.
       
  7328         ^ nil.
  7294         ^ nil.
  7329     ].
  7295     ].
  7330 "/    Transcript showCR:('defined class ' , newClass fullName , '.').
  7296     newClass classLoader: jClassLoader.
  7331     newClass classLoader:jClassLoader.
  7297     jClass := self javaClassObjectForClass: newClass.
  7332 
  7298     ^ jClass.
  7333 "/    ('Java [info]: defined new class: ' , newClass fullName) infoPrintCR.
       
  7334 
       
  7335     jClass := self javaClassObjectForClass:newClass.
       
  7336     ^ jClass
       
  7337 
  7299 
  7338     "Created: / 7.1.1998 / 12:35:10 / cg"
  7300     "Created: / 7.1.1998 / 12:35:10 / cg"
  7339     "Modified: / 24.1.1998 / 15:26:21 / cg"
  7301     "Modified: / 24.1.1998 / 15:26:21 / cg"
  7340 !
  7302 !
  7341 
  7303 
  7439     "/ introduced with jdk1.2 ... (sigh)
  7401     "/ introduced with jdk1.2 ... (sigh)
  7440 
  7402 
  7441     "Created: / 27.1.1998 / 18:37:08 / cg"
  7403     "Created: / 27.1.1998 / 18:37:08 / cg"
  7442 !
  7404 !
  7443 
  7405 
  7444 _ClassLoader_resolveClass0:nativeContext 
  7406 _ClassLoader_resolveClass0: nativeContext 
  7445     "resolve a new class as previously created by defineClass0"
  7407     "resolve a new class as previously created by defineClass0"
  7446     
  7408     
  7447     |jClassLoader jCls cls loaderStub anyUnresolved|
  7409     | jClassLoader  jCls  cls anyUnresolved |
  7448 
  7410 
  7449     jClassLoader := nativeContext receiver.
  7411     jClassLoader := nativeContext receiver.
  7450     jCls := nativeContext argAt:1.
  7412     jCls := nativeContext argAt: 1.
  7451     jCls isNil ifTrue:[
  7413     jCls isNil ifTrue: [
  7452         self halt.
  7414         self halt.
  7453         ^ nil
  7415         ^ nil
  7454     ].
  7416     ].
  7455     cls := self reflection javaClassObjectForClass:jCls.
  7417     cls := self reflection javaClassObjectForClass: jCls.
  7456     cls isNil ifTrue:[
  7418     cls isNil ifTrue: [
  7457         self halt.
  7419         self halt.
  7458         ^ nil
  7420         ^ nil
  7459     ].
  7421     ].
  7460     ('JavaVM [info]: resolving class ' , cls fullName , ' ...') infoPrintCR.
  7422     ('JavaVM [info]: resolving class ' , cls fullName , ' ...') infoPrintCR.
  7461     JavaClassReader classLoaderQuerySignal answer:jClassLoader
  7423     JavaClassReader classLoaderQuerySignal answer: jClassLoader
  7462         do:[
  7424         do: [
  7463             JavaClassReader resolveClass:cls.
  7425         "correct this so it works with new resolving"
       
  7426         self breakPoint:#mh.
       
  7427             JavaClassReader resolveClass: cls.
  7464             
  7428             
  7465 "/        JavaClassReader postLoadActions:true.
  7429 "/        JavaClassReader postLoadActions:true.
  7466             
  7430             
  7467             anyUnresolved := false.
  7431             anyUnresolved := false.
  7468             cls constantPool do:[:entry | 
  7432             cls constantPool do: [
  7469                 (entry isMemberOf:JavaUnresolvedClassConstant) ifTrue:[
  7433                 :entry | 
  7470                     self halt:'debugHalt'.
  7434                 (entry isMemberOf: JavaUnresolvedClassConstant) ifTrue: [
       
  7435                     self halt: 'debugHalt'.
  7471                     entry preResolve.
  7436                     entry preResolve.
  7472                     self halt:'debugHalt'.
  7437                     self halt: 'debugHalt'.
  7473                     anyUnresolved := true.
  7438                     anyUnresolved := true.
  7474                 ]
  7439                 ]
  7475             ]
  7440             ]
  7476         ].
  7441         ].
  7477     anyUnresolved ifTrue:[
  7442     anyUnresolved ifTrue: [
  7478         jClassLoader notNil ifTrue:[
  7443         jClassLoader notNil ifTrue: [
  7479             "/ any unresolved left -> try resolving with standard loader
  7444             "/ any unresolved left -> try resolving with standard loader
  7480             JavaClassReader classLoaderQuerySignal answer:nil
  7445             JavaClassReader classLoaderQuerySignal answer: nil
  7481                 do:[
  7446                 do: [
  7482                     JavaClassReader postLoadActions:true.
  7447                     cls constantPool do: [
  7483                     cls constantPool do:[:entry | 
  7448                         :entry | 
  7484                         (entry isMemberOf:JavaUnresolvedClassConstant) ifTrue:[
  7449                         (entry isMemberOf: JavaUnresolvedClassConstant) ifTrue: [
  7485                             self halt:'debugHalt'.
  7450                             self halt: 'debugHalt'.
  7486                             entry preResolve.
  7451                             entry preResolve.
  7487                             self halt:'debugHalt'.
  7452                             self halt: 'debugHalt'.
  7488                         ]
  7453                         ]
  7489                     ]
  7454                     ]
  7490                 ]
  7455                 ]
  7491         ].
  7456         ].
  7492     ].
  7457     ].