JavaVM.st
branchdevelopment
changeset 1876 482ad5ebe5da
parent 1867 cd4ec4fbe798
child 1877 14cbc77e9aaa
equal deleted inserted replaced
1875:eaf900eded11 1876:482ad5ebe5da
  4423 
  4423 
  4424     "Created: / 10-08-2011 / 14:02:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4424     "Created: / 10-08-2011 / 14:02:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4425 !
  4425 !
  4426 
  4426 
  4427 commonOpen:nativeContext forAppend:forAppend
  4427 commonOpen:nativeContext forAppend:forAppend
  4428     |fs fd fn name dir stream fileNo readonly|
  4428     |fs fd fn nm dir stream fileNo readonly|
  4429 
  4429 
  4430     fs := nativeContext receiver.
  4430     fs := nativeContext receiver.
  4431     fd := fs instVarNamed:'fd'.
  4431     fd := fs instVarNamed:'fd'.
  4432     (fd instVarNamed:'fd') ~~ -1 ifTrue:[
  4432     (fd instVarNamed:'fd') ~~ -1 ifTrue:[
  4433         "/self halt:'file already open'.
  4433         "/self halt:'file already open'.
  4434         self internalError:'file already open'.
  4434         self internalError:'file already open'.
  4435         ^ self.
  4435         ^ self.
  4436     ].
  4436     ].
  4437 
  4437 
  4438     name := nativeContext argAt:1.
  4438     nm := nativeContext argAt:1.
  4439     name := Java as_ST_String:name.
  4439     nm := Java as_ST_String:nm.
  4440 
  4440 
  4441     FileOpenTrace ifTrue:[
  4441     FileOpenTrace ifTrue:[
  4442         ('JAVA: opening ' , name) infoPrintCR.
  4442         ('JAVA: opening ' , nm) infoPrintCR.
  4443     ].
  4443     ].
  4444 
  4444 
  4445     fn := name utf8Decoded asFilename.
  4445     fn := nm utf8Decoded asFilename.
  4446     dir := fn directory pathName.
  4446     dir := fn directory pathName.
  4447 
  4447 
  4448     readonly := false.
  4448     readonly := false.
  4449 "/    (PermittedDirectories notNil
  4449 "/    (PermittedDirectories notNil
  4450 "/    and:[PermittedDirectories includes:dir]) ifFalse:[
  4450 "/    and:[PermittedDirectories includes:dir]) ifFalse:[
  4492         ] ifFalse:[
  4492         ] ifFalse:[
  4493             stream := self commonOpenStreamUsing:[fn writeStream].
  4493             stream := self commonOpenStreamUsing:[fn writeStream].
  4494         ]
  4494         ]
  4495     ].
  4495     ].
  4496     stream isNil ifTrue:[
  4496     stream isNil ifTrue:[
  4497         self throwIOExceptionWithMessage:('cannot open ' , name , ' for writing').
  4497         self throwIOExceptionWithMessage:('cannot open ' , nm , ' for writing').
  4498     ].
  4498     ].
       
  4499     stream binary.
  4499 
  4500 
  4500     fileNo := self addOpenFile:stream.
  4501     fileNo := self addOpenFile:stream.
  4501 
  4502 
  4502     FileOpenTrace ifTrue:[
  4503     FileOpenTrace ifTrue:[
  4503         ('JAVA: opened ' , name , ' as FD ' , fileNo printString , ' for writing') infoPrintCR.
  4504         ('JAVA: opened ' , nm , ' as FD ' , fileNo printString , ' for writing') infoPrintCR.
  4504     ].
  4505     ].
  4505 
  4506 
  4506     fd instVarNamed:'fd' put:fileNo.
  4507     fd instVarNamed:'fd' put:fileNo.
  4507 
  4508 
  4508     "Kludge for finalization..."
  4509     "Kludge for finalization..."
  4509     fs finalizationLobby registerChange: fs
  4510     fs finalizationLobby registerChange: fs
  4510 
  4511 
  4511     "Created: / 07-04-1998 / 19:14:09 / cg"
  4512     "Created: / 07-04-1998 / 19:14:09 / cg"
  4512     "Modified: / 04-01-1999 / 14:34:42 / cg"
  4513     "Modified: / 04-01-1999 / 14:34:42 / cg"
  4513     "Modified: / 23-07-2012 / 19:38:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4514     "Modified: / 12-12-2012 / 23:43:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4514 !
  4515 !
  4515 
  4516 
  4516 commonOpenStreamUsing: aBlock
  4517 commonOpenStreamUsing: aBlock
  4517     "Helper for opening a stream. Catches 'too many files' open error
  4518     "Helper for opening a stream. Catches 'too many files' open error
  4518      and tries to GC to cleanup if necessary"
  4519      and tries to GC to cleanup if necessary"
  5851         |file byte|
  5852         |file byte|
  5852 
  5853 
  5853     file := self validateFile:(nativeContext receiver).
  5854     file := self validateFile:(nativeContext receiver).
  5854 
  5855 
  5855     file == Stdin ifTrue:[
  5856     file == Stdin ifTrue:[
  5856 	file := StdinReplacementFileQuerySignal raiseRequest.
  5857         file := StdinReplacementFileQuerySignal raiseRequest.
  5857 	file isNil ifTrue:[
  5858         file isNil ifTrue:[
  5858 	    ^ -1
  5859             ^ -1
  5859 	]
  5860         ]
  5860     ].
  5861     ].
  5861 
  5862 
  5862     FileIOTrace ifTrue:[
  5863     FileIOTrace ifTrue:[
  5863 	('JAVA: read 1 byte from ' , file pathName) infoPrintCR.
  5864         ('JAVA: read 1 byte from ' , file pathName) infoPrintCR.
  5864     ].
  5865     ].
  5865 
  5866 
  5866     byte := file nextByte.
  5867     byte := file nextByte.
  5867     byte isNil ifTrue:[
  5868     byte isNil ifTrue:[
  5868 	^ -1
  5869         ^ -1
       
  5870     ].
       
  5871     "Java bytes are signed"
       
  5872     byte > 127 ifTrue:[
       
  5873         byte := 
       
  5874             byte"/ - 255.
  5869     ].
  5875     ].
  5870     ^ byte
  5876     ^ byte
  5871 
  5877 
  5872     "Created: / 5.1.1998 / 02:17:08 / cg"
  5878     "Created: / 05-01-1998 / 02:17:08 / cg"
  5873     "Modified: / 5.1.1998 / 02:17:25 / cg"
  5879     "Modified (format): / 12-12-2012 / 23:53:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  5874 !
  5880 !
  5875 
  5881 
  5876 _java_io_FileInputStream_readBytes: nativeContext
  5882 _java_io_FileInputStream_readBytes: nativeContext
  5877 
  5883 
  5878     <javanative: 'java/io/FileInputStream' name: 'readBytes'>
  5884     <javanative: 'java/io/FileInputStream' name: 'readBytes'>
  5962      public native void write(int b) throws IOException;
  5968      public native void write(int b) throws IOException;
  5963     "
  5969     "
  5964 
  5970 
  5965     | fdobj fd stream b |
  5971     | fdobj fd stream b |
  5966     b := nativeContext argAt:1.
  5972     b := nativeContext argAt:1.
       
  5973     "/Java byte is signed, make it integer in 0-255"
       
  5974     b < 0 ifTrue:[b := b + 255].
  5967     fdobj := (nativeContext receiver instVarNamed: #fd).
  5975     fdobj := (nativeContext receiver instVarNamed: #fd).
  5968     fd    := fdobj instVarNamed: #fd.
  5976     fd    := fdobj instVarNamed: #fd.
  5969     stream := self getOpenFileAt:fd.
  5977     stream := self getOpenFileAt:fd.
  5970     [
  5978     [
  5971         stream nextPut: b asCharacter
  5979         stream nextPut: b
  5972     ] on: Error do:[:ex|
  5980     ] on: Error do:[:ex|
  5973         self throwIOExceptionWithMessage: ex description
  5981         self throwIOExceptionWithMessage: ex description
  5974     ]
  5982     ]
  5975 
  5983 
  5976     "Modified: / 10-08-2011 / 14:15:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  5984     "Modified: / 12-12-2012 / 23:46:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  5977 !
  5985 !
  5978 
  5986 
  5979 _java_io_FileOutputStream_writeBytes: nativeContext
  5987 _java_io_FileOutputStream_writeBytes: nativeContext
  5980 
  5988 
  5981     <javanative: 'java/io/FileOutputStream' name: 'writeBytes'>
  5989     <javanative: 'java/io/FileOutputStream' name: 'writeBytes'>
  7560     "
  7568     "
  7561     | f |
  7569     | f |
  7562     f := aJavaContext argAt:1.
  7570     f := aJavaContext argAt:1.
  7563     f class == Float ifFalse:[ ^self throwIllegalArgumentException: 'Not a float'].
  7571     f class == Float ifFalse:[ ^self throwIllegalArgumentException: 'Not a float'].
  7564 %{
  7572 %{
  7565     {
  7573     __int64__ *i = &(__floatVal(f));
  7566         double d = __floatVal(f);
  7574     RETURN (__MKINT64( i ));
  7567         RETURN (__MKINT64( &d ));
       
  7568     }
       
  7569 %}.
  7575 %}.
  7570     self primitiveFailed.
  7576     self primitiveFailed.
  7571 
  7577 
  7572     "Created: / 10-11-2010 / 14:48:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7578     "Created: / 10-11-2010 / 14:48:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7573     "Modified: / 11-12-2012 / 22:32:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7579     "Modified: / 15-12-2012 / 14:40:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7574 !
  7580 !
  7575 
  7581 
  7576 _java_lang_Double_longBitsToDouble: nativeContext
  7582 _java_lang_Double_longBitsToDouble: nativeContext
  7577 
  7583 
  7578     <javanative: 'java/lang/Double' name: 'longBitsToDouble'>
  7584     <javanative: 'java/lang/Double' name: 'longBitsToDouble'>
  7583     f := Float new.
  7589     f := Float new.
  7584 %{
  7590 %{
  7585     __floatVal(f) = (double)(__longIntVal(i));
  7591     __floatVal(f) = (double)(__longIntVal(i));
  7586 %}.
  7592 %}.
  7587     ^f
  7593     ^f
  7588 
       
  7589     "Created: / 04-01-1998 / 01:45:00 / cg"
       
  7590     "Modified: / 11-12-2012 / 21:58:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  7591 !
  7594 !
  7592 
  7595 
  7593 _java_lang_Float_floatToRawIntBits: aJavaContext
  7596 _java_lang_Float_floatToRawIntBits: aJavaContext
  7594 
  7597 
  7595     <javanative: 'java/lang/Float' name: 'floatToRawIntBits'>
  7598     <javanative: 'java/lang/Float' name: 'floatToRawIntBits'>
  7629      * @param   value   a floating-point number.
  7632      * @param   value   a floating-point number.
  7630      * @return the bits that represent the floating-point number.
  7633      * @return the bits that represent the floating-point number.
  7631      * @since 1.3
  7634      * @since 1.3
  7632      */
  7635      */
  7633     "
  7636     "
  7634     | f |
  7637     | f b |
  7635     f := aJavaContext argAt:1.
  7638     f := aJavaContext argAt:1.
  7636     f class == ShortFloat ifFalse:[ self throwIllegalArgumentException: 'Not a float'].
  7639     f class == ShortFloat ifFalse:[ self throwIllegalArgumentException: 'Not a float'].
  7637 %{
  7640 %{
  7638     {
  7641         int i;
  7639         int *i = &(__shortFloatVal(f));
  7642         i = (int)(__shortFloatVal(f));
  7640         printf(">> F2raw bits: f=%f i=0x%04x\n", __shortFloatVal(f), *i);
  7643         RETURN (__MKINT(i));
  7641         RETURN (__MKINT(*i));
       
  7642     }
       
  7643 %}.
  7644 %}.
  7644     self primitiveFailed.
  7645     self primitiveFailed.
  7645 
  7646 
  7646     "Created: / 09-11-2010 / 20:59:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7647     "Created: / 09-11-2010 / 20:59:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7647     "Modified (format): / 11-12-2012 / 21:56:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7648     "Modified: / 07-08-2011 / 21:45:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  7648 !
  7649 !
  7649 
  7650 
  7650 _java_lang_Float_intBitsToFloat: nativeContext
  7651 _java_lang_Float_intBitsToFloat: nativeContext
  7651 
  7652 
  7652     <javanative: 'java/lang/Float' name: 'intBitsToFloat(I)F'>
  7653     <javanative: 'java/lang/Float' name: 'intBitsToFloat(I)F'>
  7657     f := ShortFloat new.
  7658     f := ShortFloat new.
  7658 %{
  7659 %{
  7659     __shortFloatVal(f) = (float)(__intVal(i));
  7660     __shortFloatVal(f) = (float)(__intVal(i));
  7660 %}.
  7661 %}.
  7661     ^f
  7662     ^f
  7662 
       
  7663     "Modified: / 11-12-2012 / 21:53:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  7664 !
  7663 !
  7665 
  7664 
  7666 _java_lang_Object_clone: nativeContext
  7665 _java_lang_Object_clone: nativeContext
  7667 
  7666 
  7668     <javanative: 'java/lang/Object' name: 'clone'>
  7667     <javanative: 'java/lang/Object' name: 'clone'>