-
-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise resolver and promise result #76
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e4da54
Promise resolver and promise result
rogchap 136ea34
Merge branch 'master' into promise
rogchap 9675c5e
update persistent value to match master
rogchap bccfa1d
Add docs, test and Exception types
rogchap 649ed14
remove the exception error functions
rogchap 1944a6e
add test for when value is not a promise
rogchap d233d29
update changelog
rogchap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
// #include <stdlib.h> | ||
// #include "v8go.h" | ||
import "C" | ||
|
||
// Error creates a generic error message. | ||
func Error(msg string) *Value { | ||
panic("not implemented") | ||
} | ||
|
||
// RangeError creates a range error message. | ||
func RangeError(msg string) *Value { | ||
panic("not implemented") | ||
} | ||
|
||
// ReferenceError creates a reference error message. | ||
func ReferenceError(msg string) *Value { | ||
panic("not implemented") | ||
} | ||
|
||
// SyntaxError creates a syntax error message. | ||
func SyntaxError(msg string) *Value { | ||
panic("not implemented") | ||
} | ||
|
||
// TypeError creates a type error message. | ||
func TypeError(msg string) *Value { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package v8go | ||
|
||
// #include <stdlib.h> | ||
// #include "v8go.h" | ||
import "C" | ||
import ( | ||
"errors" | ||
"runtime" | ||
) | ||
|
||
// PromiseState | ||
type PromiseState int | ||
|
||
const ( | ||
Pending PromiseState = iota | ||
Fulfilled | ||
Rejected | ||
) | ||
|
||
// PromiseResolver | ||
type PromiseResolver struct { | ||
*Object | ||
prom *Promise | ||
} | ||
|
||
// Promise | ||
type Promise struct { | ||
*Object | ||
} | ||
|
||
// MewPromiseResolver | ||
func NewPromiseResolver(ctx *Context) (*PromiseResolver, error) { | ||
if ctx == nil { | ||
return nil, errors.New("v8go: Context is required") | ||
} | ||
ptr := C.NewPromiseResolver(ctx.ptr) | ||
val := &Value{ptr, ctx} | ||
runtime.SetFinalizer(val, (*Value).finalizer) | ||
return &PromiseResolver{&Object{val}, nil}, nil | ||
} | ||
|
||
// GetPromise | ||
func (r *PromiseResolver) GetPromise() *Promise { | ||
if r.prom == nil { | ||
ptr := C.PromiseResolverGetPromise(r.ptr) | ||
val := &Value{ptr, r.ctx} | ||
runtime.SetFinalizer(val, (*Value).finalizer) | ||
r.prom = &Promise{&Object{val}} | ||
} | ||
return r.prom | ||
} | ||
|
||
// Resolve | ||
func (r *PromiseResolver) Resolve(val Valuer) bool { | ||
r.ctx.register() | ||
defer r.ctx.deregister() | ||
return C.PromiseResolverResolve(r.ptr, val.value().ptr) != 0 | ||
} | ||
|
||
// Reject | ||
func (r *PromiseResolver) Reject(err *Value) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not change this to Valuer? |
||
r.ctx.register() | ||
defer r.ctx.deregister() | ||
return C.PromiseResolverReject(r.ptr, err.ptr) != 0 | ||
} | ||
|
||
// State | ||
func (p *Promise) State() PromiseState { | ||
return PromiseState(C.PromiseState(p.ptr)) | ||
} | ||
|
||
// Result | ||
func (p *Promise) Result() *Value { | ||
ptr := C.PromiseResult(p.ptr) | ||
val := &Value{ptr, p.ctx} | ||
runtime.SetFinalizer(val, (*Value).finalizer) | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nite: lowercase?