changed the compiler to allow a character literal within a byte array literal

This commit is contained in:
hyunghwan.chung
2017-12-20 16:25:20 +00:00
parent d8b36bdf66
commit 4fe38f883c
4 changed files with 203 additions and 48 deletions

View File

@ -3,11 +3,16 @@
class Socket(Object) from 'sck'
{
var handle := -1.
var insem, outsem.
var(#get,#set) inputAction, outputAction.
method(#primitive) open(domain, type, proto).
method(#primitive) _close.
method(#primitive) connect(a,b,c).
method(#primitive) endConnect: xxx.
method(#primitive) endConnect.
method(#primitive) readBytes: bytes.
method(#primitive) writeBytes: bytes.
}
(* TODO: generate these domain and type from the C header *)
@ -39,6 +44,19 @@ extend Socket
{
## this primitive method may return failure.
## but ignore it here.
if (self.insem)
{
System unsignal: self.insem.
System removeAsyncSemaphore: self.insem.
self.insem := nil.
}.
if (self.outsem)
{
System unsignal: self.outsem.
System removeAsyncSemaphore: self.outsem.
self.outsem := nil.
}.
self _close.
self.handle := -1.
}
@ -54,9 +72,13 @@ extend Socket
sa := [:sem |
System unsignal: s1.
System unsignal: s2.
'UNSIGNALLLING ...........' dump.
System removeAsyncSemaphore: s1.
System removeAsyncSemaphore: s2.
connectBlock value: (sem == s1)
'FINALIZING CONNECT' dump.
self endConnect.
connectBlock value: self value: (sem == s1)
].
s1 signalAction: sa.
@ -74,34 +96,51 @@ extend Socket
]
}
method asyncRead: readBlock
method watchInput
{
| s1 s2 |
s1 := Semaphore new.
s2 := Semaphore new.
if (self.insem isNil)
{
self.insem := Semaphore new.
self.insem signalAction: [:sem | self.inputAction value: self value: true].
System signal: self.insem onInput: self.handle.
System addAsyncSemaphore: self.insem.
}
else
{
self.insem signalAction: [:sem | self.inputAction value: self value: true].
}
s1 signalAction: [:sem | readBlock value: true].
s2 signalAction: [:sem | readBlock value: false].
System signal: s1 onInput: self.handle.
System signal: s2 afterSecs: 10.
###s2 := Semaphore new.
###s2 signalAction: [:sem | inputActionBlock value: self value: false].
###System signal: s2 afterSecs: 10.
}
(*
method asyncWrite:
method unwatchInput
{
| s1 s2 |
s1 := Semaphore new.
s2 := Semaphore new.
s1 signalAction: [:sem | writeBlock value: true].
s2 signalAction: [:sem | writeBlock value: false].
System signal: s1 onOutput: self.handle.
System signal: s2 afterSecs: 10.
System unsignal: self.insem.
System removeAsyncSemaphore: self.insem.
}
*)
method watchOutput
{
if (self.outsem isNil)
{
self.outsem := Semaphore new.
self.outsem signalAction: [:sem | self.outputAction value: self value: true].
System signal: self.outsem onOutput: self.handle.
System addAsyncSemaphore: self.outsem.
}
else
{
self.outsem signalAction: [:sem | self.outputAction value: self value: true].
}
}
method unwatchOutput
{
System unsignal: self.outsem.
System removeAsyncSemaphore: self.outsem.
}
}
@ -109,32 +148,60 @@ class MyObject(Object)
{
method(#class) main
{
| s |
| s conact inact outact |
inact := [:sck :state |
| data n |
data := ByteArray new: 100.
n := sck readBytes: data.
if (n == 0)
{
sck close.
}.
(n asString & ' bytes read') dump.
data dump.
].
outact := [:sck :state |
if (state)
{
sck writeBytes: #[ $h $e $l $l $o ].
}
else
{
}
].
conact := [:sck :state |
if (state)
{
'CONNECTED NOW.............' dump.
##s onOutputDo: outact.
s writeBytes: #[ $h $e $l $l $o ].
s watchInput.
}
else
{
'UNABLE TO CONNECT............' dump.
}
].
## ------------------------------------------------------
[
s := Socket domain: Socket.Domain.INET type: Socket.Type.STREAM.
s asyncConnect: [:result |
##s endConnect: result.
##s beginRead: xxx.
if (result)
{
s endConnect: result.
'CONNECTED NOW.............' dump.
s asyncRead: [:data |
data dump.
]
}
else
{
'UNABLE TO CONNECT............' dump.
}
].
s inputAction: inact; outputAction: outact.
s asyncConnect: conact.
while (true)
{
System handleAsyncEvent.
}.
s close dump.
] on: Exception do: [:ex | ('Exception - ' & ex messageText) dump ].
'----- END OF MAIN ------' dump.