diff options
author | Ian Lance Taylor <iant@golang.org> | 2012-08-03 18:08:43 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2012-08-03 18:08:43 -0700 |
commit | 6fa38e5e0a2ceb83871d22e27a80fca271f26a73 (patch) | |
tree | 2db3e6105a2b21cacd9e395b7a771fce761222ef /misc/swig | |
parent | dee5adcf74d249adf1d6ba83eb906dbb60e2ee4f (diff) | |
download | go-6fa38e5e0a2ceb83871d22e27a80fca271f26a73.tar.gz go-6fa38e5e0a2ceb83871d22e27a80fca271f26a73.zip |
cmd/go, go/build, misc/swig: add SWIG support to Go tool
R=adg, rsc, franciscossouza, seb.binet, gen.battle
CC=golang-dev
https://golang.org/cl/5845071
Diffstat (limited to 'misc/swig')
-rw-r--r-- | misc/swig/callback/Makefile | 17 | ||||
-rw-r--r-- | misc/swig/callback/callback.go | 11 | ||||
-rw-r--r-- | misc/swig/callback/callback_test.go | 34 | ||||
-rw-r--r-- | misc/swig/callback/run.go | 39 | ||||
-rw-r--r-- | misc/swig/stdio/file.swig | 15 | ||||
-rw-r--r-- | misc/swig/stdio/file_test.go | 22 | ||||
-rw-r--r-- | misc/swig/stdio/hello.go | 11 |
7 files changed, 81 insertions, 68 deletions
diff --git a/misc/swig/callback/Makefile b/misc/swig/callback/Makefile deleted file mode 100644 index 0ca33ef604..0000000000 --- a/misc/swig/callback/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2011 The Go Authors. All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -include ../../../src/Make.inc - -TARG=swig/callback -SWIGFILES=\ - callback.swigcxx - -CLEANFILES+=run - -include ../../../src/Make.pkg - -%: install %.go - $(GC) $(GCFLAGS) $(GCIMPORTS) $*.go - $(LD) $(SWIG_RPATH) -o $@ $*.$O diff --git a/misc/swig/callback/callback.go b/misc/swig/callback/callback.go new file mode 100644 index 0000000000..39c1719d24 --- /dev/null +++ b/misc/swig/callback/callback.go @@ -0,0 +1,11 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package callback + +type GoCallback struct{} + +func (p *GoCallback) Run() string { + return "GoCallback.Run" +} diff --git a/misc/swig/callback/callback_test.go b/misc/swig/callback/callback_test.go new file mode 100644 index 0000000000..cf008fb540 --- /dev/null +++ b/misc/swig/callback/callback_test.go @@ -0,0 +1,34 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package callback_test + +import ( + "../callback" + "testing" +) + +func TestCall(t *testing.T) { + c := callback.NewCaller() + cb := callback.NewCallback() + + c.SetCallback(cb) + s := c.Call() + if s != "Callback::run" { + t.Errorf("unexpected string from Call: %q", s) + } + c.DelCallback() +} + +func TestCallback(t *testing.T) { + c := callback.NewCaller() + cb := callback.NewDirectorCallback(&callback.GoCallback{}) + c.SetCallback(cb) + s := c.Call() + if s != "GoCallback.Run" { + t.Errorf("unexpected string from Call with callback: %q", s) + } + c.DelCallback() + callback.DeleteDirectorCallback(cb) +} diff --git a/misc/swig/callback/run.go b/misc/swig/callback/run.go deleted file mode 100644 index b3f13ad908..0000000000 --- a/misc/swig/callback/run.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "fmt" - "swig/callback" -) - -type GoCallback struct{} - -func (p *GoCallback) Run() string { - return "GoCallback.Run" -} - -func main() { - c := callback.NewCaller() - cb := callback.NewCallback() - - c.SetCallback(cb) - s := c.Call() - fmt.Println(s) - if s != "Callback::run" { - panic(s) - } - c.DelCallback() - - cb = callback.NewDirectorCallback(&GoCallback{}) - c.SetCallback(cb) - s = c.Call() - fmt.Println(s) - if s != "GoCallback.Run" { - panic(s) - } - c.DelCallback() - callback.DeleteDirectorCallback(cb) -} diff --git a/misc/swig/stdio/file.swig b/misc/swig/stdio/file.swig index 57c623f8f7..8ba341d089 100644 --- a/misc/swig/stdio/file.swig +++ b/misc/swig/stdio/file.swig @@ -6,6 +6,19 @@ %{ #include <stdio.h> +#include <stdlib.h> %} -int puts(const char *); +%typemap(gotype) const char * "string" +%typemap(in) const char * %{ + $1 = malloc($input.n + 1); + memcpy($1, $input.p, $input.n); + $1[$input.n] = '\0'; +%} +%typemap(freearg) const char * %{ + free($1); +%} + +FILE *fopen(const char *name, const char *mode); +int fclose(FILE *); +int fgetc(FILE *); diff --git a/misc/swig/stdio/file_test.go b/misc/swig/stdio/file_test.go new file mode 100644 index 0000000000..6478a7cf37 --- /dev/null +++ b/misc/swig/stdio/file_test.go @@ -0,0 +1,22 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package file + +import "testing" + +// Open this file itself and verify that the first few characters are +// as expected. +func TestRead(t *testing.T) { + f := Fopen("file_test.go", "r") + if f == nil { + t.Fatal("fopen failed") + } + if Fgetc(f) != '/' || Fgetc(f) != '/' || Fgetc(f) != ' ' || Fgetc(f) != 'C' { + t.Error("read unexpected characters") + } + if Fclose(f) != 0 { + t.Error("fclose failed") + } +} diff --git a/misc/swig/stdio/hello.go b/misc/swig/stdio/hello.go deleted file mode 100644 index eec2942786..0000000000 --- a/misc/swig/stdio/hello.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import "swig/file" - -func main() { - file.Puts("Hello, world") -} |