Skip to content

Commit

Permalink
Get strings from message
Browse files Browse the repository at this point in the history
  • Loading branch information
stroiman committed Dec 5, 2024
1 parent b2124ea commit c01c392
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
12 changes: 10 additions & 2 deletions function_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ func NewFunctionTemplate(iso *Isolate, callback FunctionCallback) *FunctionTempl
// NewFunctionTemplateWithError creates a FunctionTemplate for a given
// callback. If the callback returns an error, it will be thrown as a
// JS error.
func NewFunctionTemplateWithError(iso *Isolate, callback FunctionCallbackWithError) *FunctionTemplate {
func NewFunctionTemplateWithError(
iso *Isolate,
callback FunctionCallbackWithError,
) *FunctionTemplate {
if iso == nil {
panic("nil Isolate argument not supported")
}
Expand Down Expand Up @@ -111,7 +114,12 @@ func (tmpl *FunctionTemplate) GetFunction(ctx *Context) *Function {
// to workaround an ERROR_COMMITMENT_LIMIT error on windows that was detected in CI.
//
//export goFunctionCallback
func goFunctionCallback(ctxref int, cbref int, thisAndArgs *C.ValuePtr, argsCount int) (rval C.ValuePtr, rerr C.ValuePtr) {
func goFunctionCallback(
ctxref int,
cbref int,
thisAndArgs *C.ValuePtr,
argsCount int,
) (rval C.ValuePtr, rerr C.ValuePtr) {
ctx := getContext(ctxref)

this := *thisAndArgs
Expand Down
19 changes: 6 additions & 13 deletions inspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ class InspectorClient : public V8InspectorClient {
int _callbackRef;

public:
InspectorClient(int callbackRef) {
_callbackRef = callbackRef;
printf("Client from C++");
}
InspectorClient(int callbackRef) { _callbackRef = callbackRef; }
void consoleAPIMessage(int contextGroupId,
v8::Isolate::MessageErrorLevel level,
const StringView& message,
Expand All @@ -31,15 +28,11 @@ void InspectorClient::consoleAPIMessage(int contextGroupId,
unsigned lineNumber,
unsigned columnNumber,
V8StackTrace*) {
printf("Hello from C++");
goHandleConsoleAPIMessageCallback(_callbackRef);
// if (message.is8Bit()) {
// goConsoleAPIMessageUtf8(level, lineNumber, (char*)message.characters8());
// } else {
// goConsoleAPIMessageUtf16(level, lineNumber,
// (char*)message.characters16(),
// message.length() * 2);
// }
StringViewData msg;
msg.is8bit = message.is8Bit();
msg.data = message.characters8();
msg.length = message.length();
goHandleConsoleAPIMessageCallback(_callbackRef, contextGroupId, level, msg);
}

extern "C" {
Expand Down
41 changes: 36 additions & 5 deletions inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import "C"
import (
"fmt"
"sync"
"unicode/utf16"
)

type MessageErrorLevel uint8

const (
ErrorLevelLog MessageErrorLevel = 1 << iota
ErrorLevelDebug
ErrorLevelInfo
ErrorLevelError
ErrorLevelWarning
ErrorLevelAll = ErrorLevelLog | ErrorLevelDebug | ErrorLevelInfo | ErrorLevelError | ErrorLevelWarning
)

type Inspector struct {
Expand All @@ -16,8 +28,6 @@ type InspectorClient struct {
handler ConsoleAPIMessageHandler
}

type MessageErrorLevel int

// registry is a simple map of int->something. Allows passing an int to C-code
// that can be used in a callback; then Go code can retrieve the right object
// afterwards.
Expand Down Expand Up @@ -60,7 +70,7 @@ func (r *registry[T]) get(id C.int) T {

type ConsoleAPIMessage struct {
contextGroupId int
errorLevel MessageErrorLevel
ErrorLevel MessageErrorLevel
Message string
url string
lineNumber int
Expand Down Expand Up @@ -111,13 +121,34 @@ func (c *InspectorClient) Dispose() {
C.DeleteInspectorClient(c.ptr)
}

func stringViewToString(d C.StringViewData) string {
if d.is8bit {
data := C.GoBytes(d.data, d.length)
return string(data)
} else {
data := C.GoBytes(d.data, d.length*2)
shorts := make([]uint16, len(data)/2)
for i := 0; i < len(data); i += 2 {
shorts[i/2] = (uint16(data[i+1]) << 8) | uint16(data[i])
}
return string(utf16.Decode(shorts))
}
}

//
//export goHandleConsoleAPIMessageCallback
func goHandleConsoleAPIMessageCallback(callbackRef C.int) {
func goHandleConsoleAPIMessageCallback(
callbackRef C.int,
contextGroupId C.int,
errorLevel C.int,
message C.StringViewData,
) {
fmt.Println("Callback from Go")
// Convert data to Go data
client := clientRegistry.get(callbackRef)
client.handler.ConsoleAPIMessage(ConsoleAPIMessage{
Message: "Hello",
Message: stringViewToString(message),
ErrorLevel: MessageErrorLevel(errorLevel),
})
// client.handleConsoleAPIMessageCallback(data)
}
8 changes: 8 additions & 0 deletions inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef v8InspectorClient* InspectorClientPtr;

typedef struct v8Isolate v8Isolate;

typedef _Bool bool;

#endif

#include <stddef.h>
Expand All @@ -43,6 +45,12 @@ extern void InspectorContextDestroyed(InspectorPtr inspector,
extern InspectorClientPtr NewInspectorClient(int callbackRef);
extern void DeleteInspectorClient(InspectorClientPtr client);

typedef struct StringViewData {
bool is8bit;
void const* data;
int length;
} StringViewData;

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
21 changes: 17 additions & 4 deletions inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,27 @@ func TestMonitorCreateDispose(t *testing.T) {
defer context.Close()
inspector.ContextCreated(context)
defer inspector.ContextDestroyed(context)
_, err := context.RunScript("console.log('Hello, world!')", "")
_, err := context.RunScript("console.log('Hello, world!'); console.error('Error, world!');", "")
if err != nil {
t.Error("Error occurred: " + err.Error())
return
}
if len(recorder.messages) != 1 {
if len(recorder.messages) != 2 {
t.Error("Expected exactly one message")
} else if recorder.messages[0].Message != "Hello, world!" {
t.Error("Expected Hello, World")
} else {
msg1 := recorder.messages[0]
msg2 := recorder.messages[1]
if msg1.ErrorLevel != v8.ErrorLevelLog {
t.Errorf("Expected Log error level. Got %d", msg1.ErrorLevel)
}
if msg2.ErrorLevel != v8.ErrorLevelError {
t.Errorf("Expected Error error level. Got: %d", msg2.ErrorLevel)
}
if msg1.Message != "Hello, world!" {
t.Errorf("Expected Hello, World, got %s", msg1.Message)
}
if msg2.Message != "Error, world!" {
t.Errorf("Expected Error, world!, got %s", msg2.Message)
}
}
}

0 comments on commit c01c392

Please sign in to comment.