Skip to content

Commit

Permalink
Merge pull request #138 from erezwanderman/fixwindows
Browse files Browse the repository at this point in the history
Fix ethereum compilation and functioning on Windows.
  • Loading branch information
obscuren committed Oct 17, 2014
2 parents 03fd832 + 7227552 commit abdf5e7
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
80 changes: 80 additions & 0 deletions ethereum/repl/console_colors_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Inspired by https://github.com/xuyu/logging/blob/master/colorful_win.go */

package ethrepl

import (
"syscall"
"unsafe"
)

type color uint16

const (
green = color(0x0002)
red = color(0x0004)
yellow = color(0x000E)
)

const (
mask = uint16(yellow | green | red)
)

var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetStdHandle = kernel32.NewProc("GetStdHandle")
procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
hStdout uintptr
initScreenInfo *consoleScreenBufferInfo
)

func setConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint16) bool {
ret, _, _ := procSetConsoleTextAttribute.Call(hConsoleOutput, uintptr(wAttributes))
return ret != 0
}

type coord struct {
X, Y int16
}

type smallRect struct {
Left, Top, Right, Bottom int16
}

type consoleScreenBufferInfo struct {
DwSize coord
DwCursorPosition coord
WAttributes uint16
SrWindow smallRect
DwMaximumWindowSize coord
}

func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo {
var csbi consoleScreenBufferInfo
ret, _, _ := procGetConsoleScreenBufferInfo.Call(hConsoleOutput, uintptr(unsafe.Pointer(&csbi)))
if ret == 0 {
return nil
}
return &csbi
}

const (
stdOutputHandle = uint32(-11 & 0xFFFFFFFF)
)

func init() {
hStdout, _, _ = procGetStdHandle.Call(uintptr(stdOutputHandle))
initScreenInfo = getConsoleScreenBufferInfo(hStdout)
}

func resetColorful() {
if initScreenInfo == nil {
return
}
setConsoleTextAttribute(hStdout, initScreenInfo.WAttributes)
}

func changeColor(c color) {
attr := uint16(0) & ^mask | uint16(c)
setConsoleTextAttribute(hStdout, attr)
}
5 changes: 4 additions & 1 deletion ethereum/repl/repl_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (self *JSRepl) PrintValue(v interface{}) {
method, _ := self.re.Vm.Get("prettyPrint")
v, err := self.re.Vm.ToValue(v)
if err == nil {
method.Call(method, v)
val, err := method.Call(method, v)
if err == nil {
fmt.Printf("%v", val)
}
}
}
57 changes: 54 additions & 3 deletions ethereum/repl/repl_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"strings"
)

func (self *JSRepl) read() {
Expand All @@ -14,11 +15,61 @@ func (self *JSRepl) read() {
if err != nil {
fmt.Println("Error reading input", err)
} else {
self.parseInput(string(str))
if (string(str) == "exit") {
self.Stop()
break
} else {
self.parseInput(string(str))
}
}
}
}

func (self *JSRepl) PrintValue(value otto.Value) {
fmt.Println(value)
func addHistory(s string) {
}

func printColored(outputVal string) {
for ; outputVal != "" ; {
codePart := ""
if (strings.HasPrefix(outputVal, "\033[32m")) {
codePart = "\033[32m"
changeColor(2)
}
if (strings.HasPrefix(outputVal, "\033[1m\033[30m")) {
codePart = "\033[1m\033[30m"
changeColor(8)
}
if (strings.HasPrefix(outputVal, "\033[31m")) {
codePart = "\033[31m"
changeColor(red)
}
if (strings.HasPrefix(outputVal, "\033[35m")) {
codePart = "\033[35m"
changeColor(5)
}
if (strings.HasPrefix(outputVal, "\033[0m")) {
codePart = "\033[0m"
resetColorful()
}
textPart := outputVal[len(codePart):len(outputVal)]
index := strings.Index(textPart, "\033")
if index == -1 {
outputVal = ""
} else {
outputVal = textPart[index:len(textPart)]
textPart = textPart[0:index]
}
fmt.Printf("%v", textPart)
}
}

func (self *JSRepl) PrintValue(v interface{}) {
method, _ := self.re.Vm.Get("prettyPrint")
v, err := self.re.Vm.ToValue(v)
if err == nil {
val, err := method.Call(method, v)
if err == nil {
printColored(fmt.Sprintf("%v", val))
}
}
}
4 changes: 3 additions & 1 deletion javascript/js_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ function pp(object) {
function prettyPrint(/* */) {
var args = arguments;
var ret = "";
for(var i = 0, l = args.length; i < l; i++) {
console.log(pp(args[i]))
ret += pp(args[i]) + "\n";
}
return ret;
}
var print = prettyPrint;
Expand Down

0 comments on commit abdf5e7

Please sign in to comment.