Skip to content

Commit

Permalink
Merge branch 'master' into avoid-some-error-returns
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap authored Sep 1, 2021
2 parents 897a325 + a674d15 commit f50ba33
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ func (fn *Function) NewInstance(args ...Valuer) (*Object, error) {
fn.ctx.deregister()
return getObject(fn.ctx, rtn), getError(rtn)
}

// Return the source map url for a function.
func (fn *Function) SourceMapUrl() *Value {
ptr := C.FunctionSourceMapUrl(fn.ptr)
return &Value{ptr, fn.ctx}
}
29 changes: 29 additions & 0 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ func TestFunctionCall(t *testing.T) {
}
}

func TestFunctionSourceMapUrl(t *testing.T) {
t.Parallel()

ctx, err := v8go.NewContext()
failIf(t, err)
_, err = ctx.RunScript("function add(a, b) { return a + b; }; //# sourceMappingURL=main.js.map", "main.js")
failIf(t, err)
addValue, err := ctx.Global().Get("add")
failIf(t, err)

fn, _ := addValue.AsFunction()

resultVal := fn.SourceMapUrl()
if resultVal.String() != "main.js.map" {
t.Errorf("expected main.js.map, got %v", resultVal.String())
}

_, err = ctx.RunScript("function sub(a, b) { return a - b; };", "")
failIf(t, err)
subValue, err := ctx.Global().Get("sub")
failIf(t, err)

subFn, _ := subValue.AsFunction()
resultVal = subFn.SourceMapUrl()
if !resultVal.IsUndefined() {
t.Errorf("expected undefined, got: %v", resultVal.DetailString())
}
}

func TestFunctionCallToGoFunc(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 12 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,18 @@ RtnValue FunctionNewInstance(ValuePtr ptr, int argc, ValuePtr args[]) {
return rtn;
}

ValuePtr FunctionSourceMapUrl(ValuePtr ptr) {
LOCAL_VALUE(ptr)
RtnValue rtn = {nullptr, nullptr};
Local<Function> fn = Local<Function>::Cast(value);
Local<Value> result = fn->GetScriptOrigin().SourceMapUrl();
m_value* rtnval = new m_value;
rtnval->iso = iso;
rtnval->ctx = ctx;
rtnval->ptr = Persistent<Value, CopyablePersistentTraits<Value>>(iso, result);
return tracked_value(ctx, rtnval);
}

/******** Exceptions *********/

ValuePtr ExceptionError(IsolatePtr iso_ptr, const char* message) {
Expand Down
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ extern ValuePtr PromiseResult(ValuePtr ptr);

extern RtnValue FunctionCall(ValuePtr ptr, int argc, ValuePtr argv[]);
RtnValue FunctionNewInstance(ValuePtr ptr, int argc, ValuePtr args[]);
ValuePtr FunctionSourceMapUrl(ValuePtr ptr);

extern ValuePtr ExceptionError(IsolatePtr iso_ptr, const char* message);
extern ValuePtr ExceptionRangeError(IsolatePtr iso_ptr, const char* message);
Expand Down

0 comments on commit f50ba33

Please sign in to comment.