// ImporterBridge is called by C to pass Importer arguments into Go land. A // Sass_Import is returned for libsass to resolve. // //export ImporterBridge func ImporterBridge(url *C.char, prev *C.char, ptr unsafe.Pointer) C.Sass_Import_List { // Retrieve the index idx := (*string)(ptr) entries, ok := globalImports.get(idx).([]ImportEntry) if !ok { fmt.Printf("failed to resolve import slice: %p\n", ptr) entries = []ImportEntry{} } parent := C.GoString(prev) rel := C.GoString(url) list := C.sass_make_import_list(1) hdr := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(list)), Len: 1, Cap: 1, } golist := *(*[]C.Sass_Import_Entry)(unsafe.Pointer(&hdr)) if body, err := GetEntry(entries, parent, rel); err == nil { ent := C.sass_make_import_entry(url, C.CString(body), nil) cent := (C.Sass_Import_Entry)(ent) golist[0] = cent } else if strings.HasPrefix(rel, "compass") { ent := C.sass_make_import_entry(url, C.CString(""), nil) cent := (C.Sass_Import_Entry)(ent) golist[0] = cent } else { ent := C.sass_make_import_entry(url, nil, nil) cent := (C.Sass_Import_Entry)(ent) golist[0] = cent } return list }
// ImporterBridge is called by C to pass Importer arguments into Go land. A // Sass_Import is returned for libsass to resolve. // //export ImporterBridge func ImporterBridge(url *C.char, prev *C.char, ptr unsafe.Pointer) C.Sass_Import_List { ctx := (*Context)(ptr) parent := C.GoString(prev) rel := C.GoString(url) list := C.sass_make_import_list(1) hdr := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(list)), Len: 1, Cap: 1, } golist := *(*[]C.Sass_Import_Entry)(unsafe.Pointer(&hdr)) if body, err := ctx.Imports.Get(parent, rel); err == nil { conts := C.CString(string(body)) ent := C.sass_make_import_entry(url, conts, nil) cent := (C.Sass_Import_Entry)(ent) golist[0] = cent } else if strings.HasPrefix(rel, "compass") { ent := C.sass_make_import_entry(url, C.CString(""), nil) cent := (C.Sass_Import_Entry)(ent) golist[0] = cent } else { ent := C.sass_make_import_entry(url, nil, nil) cent := (C.Sass_Import_Entry)(ent) golist[0] = cent } return list }
//export HeaderBridge func HeaderBridge(ptr unsafe.Pointer) C.Sass_Import_List { 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))) hdr := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(cents)), Len: len(entries), Cap: len(entries), } goents := *(*[]C.Sass_Import_Entry)(unsafe.Pointer(&hdr)) for i, ent := range entries { // Each entry needs a unique name cent := C.sass_make_import_entry( C.CString(ent.Path), C.CString(ent.Source), C.CString(ent.SrcMap)) // There is a function for modifying an import list, but a proper // slice might be more useful. // C.sass_import_set_list_entry(cents, C.size_t(i), cent) goents[i] = cent } return cents }
// HeaderBridge is called by libsass to find available custom headers // //export HeaderBridge func HeaderBridge(ptr unsafe.Pointer) C.Sass_Import_List { ctx := (*Context)(ptr) l := ctx.Headers.Len() list := C.sass_make_import_list(C.size_t(l)) hdr := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(list)), Len: l, Cap: l, } golist := *(*[]C.Sass_Import_Entry)(unsafe.Pointer(&hdr)) for i, head := range ctx.Headers.h { ent := C.sass_make_import_entry( nil, C.CString(head.Content), nil) cent := (C.Sass_Import_Entry)(ent) golist[i] = cent } return list }