Skip to content

Commit

Permalink
Merge pull request #604 from goby-lang/implement-#602
Browse files Browse the repository at this point in the history
Implement #602 and fix simple server
  • Loading branch information
st0012 authored Feb 27, 2018
2 parents 4126ab0 + 3bb3e9b commit 942c503
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
10 changes: 6 additions & 4 deletions vm/call_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type callFrame interface {
IsBlock() bool
IsSourceBlock() bool
IsRemoved() bool
setAsRemoved()
EP() *normalCallFrame
Locals() []*Pointer
LocalPtr() int
Expand All @@ -55,9 +56,7 @@ type goMethodCallFrame struct {
name string
}

func (cf *goMethodCallFrame) stopExecution() {
cf.isRemoved = true
}
func (cf *goMethodCallFrame) stopExecution() {}

type normalCallFrame struct {
*baseFrame
Expand All @@ -71,7 +70,6 @@ func (n *normalCallFrame) instructionsCount() int {
}

func (n *normalCallFrame) stopExecution() {
n.isRemoved = true
n.pc = n.instructionsCount()
}

Expand All @@ -91,6 +89,10 @@ func (b *baseFrame) IsRemoved() bool {
return b.isRemoved
}

func (b *baseFrame) setAsRemoved() {
b.isRemoved = true
}

func (b *baseFrame) IsSourceBlock() bool {
return b.isSourceBlock
}
Expand Down
13 changes: 10 additions & 3 deletions vm/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,16 @@ var builtinActions = map[operationType]*action{
*/

if cf.IsBlock() {
t.callFrameStack.pop().stopExecution() // Remove block execution frame
t.callFrameStack.pop().stopExecution() // Remove method call frame
t.callFrameStack.pop().stopExecution() // Remove block source frame
/*
1. Remove block execution frame
2. Remove method call frame
3. Remove block source frame
*/
for i := 0; i < 3; i++ {
frame := t.callFrameStack.pop()
frame.stopExecution()
frame.setAsRemoved()
}
}
},
},
Expand Down
13 changes: 11 additions & 2 deletions vm/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"path/filepath"
"unicode"

"fmt"
"github.com/fatih/structs"
"github.com/goby-lang/goby/vm/classes"
"github.com/gorilla/mux"
"strconv"
)

type request struct {
Expand Down Expand Up @@ -71,12 +73,19 @@ func builtinSimpleServerInstanceMethods() []*BuiltinMethodObject {
var serveStatic bool
server := receiver.(*RObject)

portVar, ok := server.InstanceVariables.get("@port")
portVar, ok := server.instanceVariableGet("@port")

if !ok {
port = "8080"
} else {
port = portVar.(*StringObject).value
switch p := portVar.(type) {
case *StringObject:
port = p.value
case *IntegerObject:
port = strconv.Itoa(p.value)
default:
fmt.Printf("Unexpected type %s for port setting\n", portVar.Class().Name)
}
}

log.Println("SimpleServer start listening on port: " + port)
Expand Down
63 changes: 63 additions & 0 deletions vm/simple_server_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package vm

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)

func TestServerInitialization(t *testing.T) {
Expand All @@ -28,6 +31,66 @@ func TestServerInitialization(t *testing.T) {
}
}

func TestServerSetupResponse(t *testing.T) {
serverScript := `
require "net/simple_server"
server = Net::SimpleServer.new(4000)
server.get "/" do |req, res|
res.body = req.method + " Hello World"
res.status = 200
end
server.get "/not_found" do |req, res|
res.body = String.fmt("Path \"%s\" not found", req.path)
res.status = 404
end
server.start
`
tests := []struct {
path string
expectedBody string
expectedStatus int
}{
{
"/",
"GET Hello World",
200},
{
"/not_found",
"Path \"/not_found\" not found",
404},
}

go func() {
v := initTestVM()
v.testEval(t, serverScript, getFilename())
}()

time.Sleep(1 * time.Second)

for _, tt := range tests {
resp, err := http.Get("http://localhost:4000" + tt.path)

if err != nil {
t.Fatal(err.Error())
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

if string(body) != tt.expectedBody {
t.Fatalf("Expect response body to be: \n %s, got \n %s", tt.expectedBody, string(body))
}

if resp.StatusCode != tt.expectedStatus {
t.Fatalf("Expect response status to be %d, got %d", tt.expectedStatus, resp.StatusCode)
}
}
}

func TestSetupResponseDefaultValue(t *testing.T) {
reader := strings.NewReader("")
recorder := httptest.NewRecorder()
Expand Down

0 comments on commit 942c503

Please sign in to comment.