Skip to content

Commit

Permalink
rename files, split c into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz committed Apr 23, 2011
1 parent b6d5dde commit 33e9e91
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -2,7 +2,7 @@ _cgo*
_go_*
*~
perl/_obj
perl/cgo.cgo2.o
perl/campher.cgo2.o
6.out
_gotest_*
_test*
3 changes: 1 addition & 2 deletions perl/Makefile
Expand Up @@ -15,8 +15,7 @@
include $(GOROOT)/src/Make.inc

TARG=github.com/bradfitz/campher/perl
GOFILES=perl.go
CGOFILES=cgo.go
CGOFILES=campher.go

include $(GOROOT)/src/Make.pkg

Expand Down
38 changes: 38 additions & 0 deletions perl/campher.c
@@ -0,0 +1,38 @@
static int dummy_argc = 3;
static char** dummy_argv;
static char** dummy_env;

static void campher_init() {
dummy_argv = malloc(sizeof(char*) * 3);
dummy_env = malloc(sizeof(char*) * 2);
dummy_argv[0] = "campher";
dummy_argv[1] = "-e";
dummy_argv[2] = "0";
dummy_env[0] = "FOO=bar";
dummy_env[1] = NULL;
PERL_SYS_INIT3(&dummy_argc,&dummy_argv,&dummy_env);
}

static void campher_set_context(PerlInterpreter* perl) {
PERL_SET_CONTEXT(perl);
}

static char *campher_embedding[] = { "", "-e", "0" };

static PerlInterpreter* campher_new_perl() {
PerlInterpreter* my_perl = perl_alloc();
PERL_SET_CONTEXT(my_perl);
perl_construct(my_perl);
perl_parse(my_perl, NULL, 3, campher_embedding, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_run(my_perl);
return my_perl;
}

static SV* campher_eval_pv(PerlInterpreter* my_perl, char* code) {
return eval_pv(code, TRUE);
}

static int campher_sv_int(PerlInterpreter* my_perl, SV* sv) {
return SvIVx(sv);
}
58 changes: 58 additions & 0 deletions perl/campher.go
@@ -0,0 +1,58 @@
package perl

/*
#cgo CFLAGS: -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.10/CORE
#cgo LDFLAGS: -Wl,-E -fstack-protector -L/usr/local/lib -L/usr/lib/perl/5.10/CORE -lperl -ldl -lm -lpthread -lc -lcrypt
#include <EXTERN.h>
#include <perl.h>
#include "campher.c"
*/
import "C"

import (
"unsafe"
)

func init() {
C.campher_init()
}

type Interpreter struct {
perl *_Ctypedef_PerlInterpreter
}

func (in *Interpreter) be_context() {
C.campher_set_context(in.perl)
}

func NewInterpreter() *Interpreter {
int := new(Interpreter)
int.perl = C.campher_new_perl()
// TODO: set finalizer and stuff
return int
}

type SV *C.SV

func (ip *Interpreter) Eval(str string) SV {
ip.be_context()
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
return SV(C.campher_eval_pv(ip.perl, cstr))
}

func (ip *Interpreter) EvalInt(str string) int {
ip.be_context()
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
sv := C.campher_eval_pv(ip.perl, cstr)
return int(C.campher_sv_int(ip.perl, sv))
}

func (ip *Interpreter) EvalString(str string) string {
ip.be_context()
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
//sv := C.campher_eval_pv(ip.perl, cstr)
return "foo" // int(C.campher_sv_int(ip.perl, sv))
}
3 changes: 3 additions & 0 deletions perl/perl_test.go → perl/campher_test.go
Expand Up @@ -18,4 +18,7 @@ func TestPerl(t *testing.T) {
if e, g := 123, perl.EvalInt("$foo"); e != g {
t.Errorf("$foo expected %d; got %d", e, g)
}
if e, g := "123", perl.EvalString("$foo"); e != g {
t.Errorf("$foo expected %q; got %q", e, g)
}
}
87 changes: 0 additions & 87 deletions perl/cgo.go

This file was deleted.

1 change: 0 additions & 1 deletion perl/perl.go

This file was deleted.

0 comments on commit 33e9e91

Please sign in to comment.