Skip to content

Commit

Permalink
convert header to thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
drewwells committed Sep 23, 2015
1 parent c42f6d6 commit 69ed4eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (ctx *Context) SetHeaders(opts libs.SassOptions) {
}
}
// ctx.entries = &entries
libs.BindHeader(opts, &entries)
libs.BindHeader(opts, entries)
}

type Header struct {
Expand Down
20 changes: 12 additions & 8 deletions libs/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ package libs
// }
//
import "C"
import (
"runtime"
"unsafe"
)
import "unsafe"

var globalHeaders SafeMap

func init() {
globalHeaders.init()
}

// This binds the header to the libsass header lookup
func BindHeader(opts SassOptions, entries *[]ImportEntry) {
ptr := unsafe.Pointer(entries)
// FIXME: this should be cleaned up manually later
runtime.SetFinalizer(entries, nil)
func BindHeader(opts SassOptions, entries []ImportEntry) {

idx := globalHeaders.set(entries)
ptr := unsafe.Pointer(idx)

imper := C.sass_make_importer(
C.Sass_Importer_Fn(C.SassHeaders),
C.double(0),
Expand Down
7 changes: 6 additions & 1 deletion libs/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ type ImportEntry struct {

//export HeaderBridge
func HeaderBridge(ptr unsafe.Pointer) C.Sass_Import_List {
entries := *(*[]ImportEntry)(ptr)
idx := (*string)(ptr)
entries, ok := globalHeaders.get(idx).([]ImportEntry)
if !ok {
fmt.Printf("failed to resolve header slice: %p\n", ptr)
return C.sass_make_import_list(C.size_t(1))
}

cents := C.sass_make_import_list(C.size_t(len(entries)))

Expand Down

0 comments on commit 69ed4eb

Please sign in to comment.