From 9c6a23431289de0bda4d312e7c9070bb64cd53b8 Mon Sep 17 00:00:00 2001 From: jyyi1 Date: Fri, 8 Nov 2024 16:31:12 -0500 Subject: [PATCH] export FetchResource from Go --- client/go/outline/electron/cstring.go | 26 +++++++++++++++++ client/go/outline/electron/fetch.go | 36 ++++++++++++++++++++++++ client/go/outline/electron/handle.go | 33 ++++++++++++++++++++++ client/go/outline/electron/platerr.go | 40 +++++++++++++++++++++++++++ client/go/outline/electron/platerr.h | 17 ++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 client/go/outline/electron/cstring.go create mode 100644 client/go/outline/electron/fetch.go create mode 100644 client/go/outline/electron/handle.go create mode 100644 client/go/outline/electron/platerr.go create mode 100644 client/go/outline/electron/platerr.h diff --git a/client/go/outline/electron/cstring.go b/client/go/outline/electron/cstring.go new file mode 100644 index 0000000000..67af47c632 --- /dev/null +++ b/client/go/outline/electron/cstring.go @@ -0,0 +1,26 @@ +// Copyright 2024 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +/* +#include +*/ +import "C" +import "unsafe" + +//export FreeString +func FreeString(str *C.char) { + C.free(unsafe.Pointer(str)) +} diff --git a/client/go/outline/electron/fetch.go b/client/go/outline/electron/fetch.go new file mode 100644 index 0000000000..f2dece2a81 --- /dev/null +++ b/client/go/outline/electron/fetch.go @@ -0,0 +1,36 @@ +// Copyright 2024 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +/* +#include "platerr.h" + +struct FetchResourceResult { + const char *Content; + PlatformErrorHandle Error; +}; +*/ +import "C" +import "github.com/Jigsaw-Code/outline-apps/client/go/outline" + +//export FetchResource +func FetchResource(cstr *C.char) C.struct_FetchResourceResult { + url := C.GoString(cstr) + result := outline.FetchResource(url) + return C.struct_FetchResourceResult{ + Content: C.CString(result.Content), + Error: ToCPlatformErrorHandle(result.Error), + } +} diff --git a/client/go/outline/electron/handle.go b/client/go/outline/electron/handle.go new file mode 100644 index 0000000000..94315df130 --- /dev/null +++ b/client/go/outline/electron/handle.go @@ -0,0 +1,33 @@ +// Copyright 2024 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +/* +#include +*/ +import "C" +import ( + "runtime/cgo" +) + +const NilHandle = 0 + +//export FreeHandle +func FreeHandle(ptr C.uintptr_t) { + if ptr != NilHandle { + h := cgo.Handle(ptr) + h.Delete() + } +} diff --git a/client/go/outline/electron/platerr.go b/client/go/outline/electron/platerr.go new file mode 100644 index 0000000000..2f09d90db4 --- /dev/null +++ b/client/go/outline/electron/platerr.go @@ -0,0 +1,40 @@ +// Copyright 2024 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +/* +#include "platerr.h" +*/ +import "C" +import ( + "runtime/cgo" + + "github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors" +) + +func ToCPlatformErrorHandle(err *platerrors.PlatformError) C.PlatformErrorHandle { + if err == nil { + return NilHandle + } + return C.PlatformErrorHandle(cgo.NewHandle(err)) +} + +func FromCPlatformErrorHandle(err C.PlatformErrorHandle) *platerrors.PlatformError { + if err == NilHandle { + return nil + } + h := cgo.Handle(err) + return h.Value().(*platerrors.PlatformError) +} diff --git a/client/go/outline/electron/platerr.h b/client/go/outline/electron/platerr.h new file mode 100644 index 0000000000..16750cc4f6 --- /dev/null +++ b/client/go/outline/electron/platerr.h @@ -0,0 +1,17 @@ +// Copyright 2024 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +typedef uintptr_t PlatformErrorHandle;