Skip to content

Commit

Permalink
core!: 1) add context to Importable API; 2) more iterators changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
moisespsena committed Mar 6, 2024
1 parent 361fe2b commit ca00916
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 51 deletions.
6 changes: 6 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
BuiltinValues
BuiltinItems
BuiltinCollect
BuiltinEnumerate
BuiltinIteratorInput
BuiltinVMPushWriter
BuiltinVMPopWriter
Expand Down Expand Up @@ -236,6 +237,7 @@ var BuiltinsMap = map[string]BuiltinType{
"values": BuiltinValues,
"items": BuiltinItems,
"collect": BuiltinCollect,
"enumerate": BuiltinEnumerate,
"iterator": BuiltinIterator,
"iteratorInput": BuiltinIteratorInput,
"keyValue": BuiltinKeyValue,
Expand Down Expand Up @@ -649,6 +651,10 @@ func init() {
Name: "collect",
Value: BuiltinCollectFunc,
}
BuiltinObjects[BuiltinEnumerate] = &BuiltinFunction{
Name: "enumerate",
Value: BuiltinEnumerateFunc,
}
BuiltinObjects[BuiltinIterator] = TIterator
BuiltinObjects[BuiltinIteratorInput] = &BuiltinFunction{
Name: "iteratorInput",
Expand Down
10 changes: 5 additions & 5 deletions builtins_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,12 @@ func BuiltinEnumerateFunc(c Call) (_ Object, err error) {
if _, it, err = ToIterator(c.VM, v, &c.NamedArgs); err != nil {
return
}
return TypedIteratorObject(TEnumerateIterator, WrapIterator(it, func(e *IteratorEntry) (Object, error) {
kv := e.KeyValue
e.K = i
e.V = &kv
return TypedIteratorObject(TEnumerateIterator, WrapIterator(it, func(state *IteratorState) error {
kv := state.Entry.KeyValue
state.Entry.K = i
state.Entry.V = &kv
i++
return e, nil
return nil
})), nil
}

Expand Down
28 changes: 27 additions & 1 deletion builtins_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
TIterator = &Type{Parent: TAny, TypeName: "Iterator"}
TIterabler = &Type{Parent: TAny, TypeName: "Iterabler"}
TNilIterator = &Type{Parent: TIterator, TypeName: "NilIterator"}
TStateIterator = &Type{Parent: TIterator, TypeName: "StateIterator"}
TStrIterator = &Type{Parent: TIterator, TypeName: "StrIterator"}
TRawStrIterator = &Type{Parent: TIterator, TypeName: "RawStrIterator"}
TArrayIterator = &Type{Parent: TIterator, TypeName: "ArrayIterator"}
Expand All @@ -32,7 +33,6 @@ var (
TEachIterator = &Type{Parent: TIterator, TypeName: "EachIterator"}
TMapIterator = &Type{Parent: TIterator, TypeName: "MapIterator"}
TFilterIterator = &Type{Parent: TIterator, TypeName: "FilterIterator"}
TReduceIterator = &Type{Parent: TIterator, TypeName: "ReduceIterator"}
TZipIterator = &Type{Parent: TIterator, TypeName: "ZipIterator"}
TPipedInvokeIterator = &Type{Parent: TIterator, TypeName: "PipedInvokeIterator"}
)
Expand Down Expand Up @@ -60,6 +60,17 @@ func (t *Type) AddCallerMethod(vm *VM, types MultipleObjectTypes, handler Caller
}, override)
}

func (t *Type) WithMethod(types MultipleObjectTypes, handler CallerObject, override bool) *Type {
if len(types) == 0 {
// overrides default constructor. uses Type.new to instantiate.
override = true
}
t.calllerMethods.Add(types, &CallerMethod{
CallerObject: handler,
}, override)
return t
}

func (t *Type) HasCallerMethods() bool {
return !t.calllerMethods.IsZero()
}
Expand Down Expand Up @@ -199,3 +210,18 @@ func TypesOf(obj Object) (types []ObjectType) {
}
return types
}

func init() {
TIterator.WithMethod(
MultipleObjectTypes{{nil}},
&Function{
Value: func(c Call) (o Object, err error) {
if err = c.Args.CheckLen(1); err != nil {
return
}
_, o, err = ToStateIterator(c.VM, c.Args.GetOnly(0), &c.NamedArgs)
return
},
},
true)
}
2 changes: 2 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gad

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -75,6 +76,7 @@ type (

// CompilerOptions represents customizable options for Compile().
CompilerOptions struct {
Context context.Context
ModuleMap *ModuleMap
ModulePath string
Constants []Object
Expand Down
2 changes: 1 addition & 1 deletion compiler_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ func (c *Compiler) compileImportExpr(nd *node.ImportExpr) error {

module, exists := c.getModule(moduleName)
if !exists {
mod, err := importer.Import(moduleName)
mod, err := importer.Import(c.opts.Context, moduleName)
if err != nil {
return c.error(nd, err)
}
Expand Down
6 changes: 3 additions & 3 deletions encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ func testBytecodeConstants(t *testing.T, vm *gad.VM, expected, decoded []gad.Obj
_, decIt, err := gad.ToStateIterator(vm, decoded[i], gad.NewNamedArgs())
require.NoError(t, err)

for next(decIt.Next()) {
require.True(t, next(it.Next()))
for next(decIt.Read()) {
require.True(t, next(it.Read()))
key := decIt.Key()
v1, err := gad.Val(expected[i].(gad.IndexGetter).IndexGet(vm, key))
require.NoError(t, err)
Expand All @@ -633,7 +633,7 @@ func testBytecodeConstants(t *testing.T, vm *gad.VM, expected, decoded []gad.Obj
require.Equal(t, v1, v2)
}
}
require.False(t, next(it.Next()))
require.False(t, next(it.Read()))
continue
}
require.Equalf(t, expected[i], decoded[i],
Expand Down
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var (

// ErrNotInitializable represents a not initializable type error.
ErrNotInitializable = &Error{Name: "ErrNotInitializable"}

// ErrNotWriteable represents a not writeable type error.
ErrNotWriteable = &Error{Name: "ErrNotWriteable"}
)

// NewOperandTypeError creates a new Error from ErrType.
Expand Down
7 changes: 1 addition & 6 deletions helper_foreach.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,9 @@ func (f *PipedInvokeIterator) SetHandler(handler func(state *IteratorState) erro

func (f *PipedInvokeIterator) checkNext(vm *VM, state *IteratorState) (err error) {
try:
if state.Mode == IteratorStateModeDone {
if err = IteratorStateCheck(vm, f.it, state); err != nil || state.Mode == IteratorStateModeDone {
return
}
for state.Mode == IteratorStateModeContinue {
if err = f.it.Next(vm, state); err != nil || state.Mode == IteratorStateModeDone {
return
}
}
if err = f.handler(state); err == nil {
if err = f.Call(state); state.Mode != IteratorStateModeEntry {
goto try
Expand Down
3 changes: 2 additions & 1 deletion importers/importers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package importers

import (
"context"
"errors"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (m *FileImporter) Name() string {

// Import returns the content of the path determined by Name call. Empty name
// will return an error.
func (m *FileImporter) Import(moduleName string) (any, error) {
func (m *FileImporter) Import(_ context.Context, moduleName string) (any, error) {
// Note that; moduleName == Literal()
if m.name == "" || moduleName == "" {
return nil, errors.New("invalid import call")
Expand Down

0 comments on commit ca00916

Please sign in to comment.