Merge branch 'ble' into discovery
diff --git a/impl/google/discovery/jni.go b/impl/google/discovery/jni.go
index 354593a..2eb2582 100644
--- a/impl/google/discovery/jni.go
+++ b/impl/google/discovery/jni.go
@@ -11,8 +11,12 @@
 	"encoding/binary"
 	"unsafe"
 
+	"v.io/v23/discovery"
+	"v.io/v23/security"
+	idiscovery "v.io/x/ref/lib/discovery"
+
 	jutil "v.io/x/jni/util"
-	"v.io/x/ref/lib/discovery"
+	jcontext "v.io/x/jni/v23/context"
 )
 
 // #include "jni.h"
@@ -20,9 +24,23 @@
 import "C"
 
 var (
-	classSign = jutil.ClassSign("java.lang.Class")
+	updateSign = jutil.ClassSign("io.v.v23.discovery.Update")
+
+
 	// Global reference for java.util.UUID class;
 	jUUIDClass jutil.Class
+
+	// Global reference io.v.impl.google.lib.discovery.ScanHandler
+	jScanHandlerClass jutil.Class
+
+	// Global reference io.v.v23.discovery.VDiscovery$DoneCallback
+	jServiceClass jutil.Class
+
+	// Global reference io.v.v23.security.BlessingPattern
+	jBlessingPatternClass jutil.Class
+
+	// Global reference io.v.v23.discovery.Update
+	jUpdateClass jutil.Class
 )
 
 // Init initializes the JNI code with the given Java environment. This method
@@ -33,11 +51,31 @@
 	// thread, so we aren't able to invoke FindClass in other threads.
 	var err error
 	jUUIDClass, err = jutil.JFindClass(env, "java/util/UUID")
+	if err != nil {
+		return err
+	}
 
+	jScanHandlerClass, err = jutil.JFindClass(env, "io/v/impl/google/lib/discovery/ScanHandler")
+	if err != nil {
+		return err
+	}
+
+	jServiceClass, err = jutil.JFindClass(env, "io/v/v23/discovery/Service")
+	if err != nil {
+		return err
+	}
+
+	jBlessingPatternClass, err = jutil.JFindClass(env, "io/v/v23/security/BlessingPattern")
+	if err != nil {
+		return err
+	}
+
+
+	jUpdateClass, err = jutil.JFindClass(env, "io/v/v23/discovery/Update")
 	return err
 }
 
-func convertStringtoUUID(jenv *C.JNIEnv, _ C.jclass, jName C.jstring, generator func(string) discovery.Uuid) C.jobject {
+func convertStringtoUUID(jenv *C.JNIEnv, _ C.jclass, jName C.jstring, generator func(string) idiscovery.Uuid) C.jobject {
 	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
 	name := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jName))))
 	uuid := generator(name)
@@ -55,13 +93,130 @@
 
 //export Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForInterfaceName
 func Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForInterfaceName(jenv *C.JNIEnv, jclass C.jclass, jName C.jstring) C.jobject {
-	return convertStringtoUUID(jenv, jclass, jName, discovery.NewServiceUUID)
+	return convertStringtoUUID(jenv, jclass, jName, idiscovery.NewServiceUUID)
 }
 
 //export Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForAttributeKey
 func Java_io_v_impl_google_lib_discovery_UUIDUtil_UUIDForAttributeKey(jenv *C.JNIEnv, jclass C.jclass, jName C.jstring) C.jobject {
-	converter := func(s string) discovery.Uuid {
-		return discovery.Uuid(discovery.NewAttributeUUID(s))
+	converter := func(s string) idiscovery.Uuid {
+		return idiscovery.Uuid(idiscovery.NewAttributeUUID(s))
 	}
 	return convertStringtoUUID(jenv, jclass, jName, converter)
 }
+
+
+//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeDelete
+func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_nativeDelete(jenv *C.JNIEnv, _ C.jobject, discovery C.jlong, trigger C.jlong) {
+	jutil.GoUnref(jutil.NativePtr(discovery))
+	jutil.GoUnref(jutil.NativePtr(trigger))
+}
+
+//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_advertise
+func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_advertise(jenv *C.JNIEnv, this C.jobject, jContext C.jobject, jService C.jobject, jPerms C.jobject, jCallback C.jobject) {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	ctx, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
+	if err != nil {
+		return
+	}
+	servObject := jutil.Object(uintptr(unsafe.Pointer(jService)))
+	var service discovery.Service
+	if err := jutil.GoVomCopy(env, servObject, jServiceClass, &service); err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+
+	permsArray, err := jutil.GoObjectList(env, jutil.Object(uintptr(unsafe.Pointer(jPerms))))
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	permsSlice := make([]security.BlessingPattern, len(permsArray))
+	for i, o := range permsArray {
+		if err := jutil.GoVomCopy(env, o, jBlessingPatternClass, &permsSlice[i]); err != nil {
+			jutil.JThrowV(env, err)
+			return
+		}
+	}
+	thisObject := jutil.Object(uintptr(unsafe.Pointer(this)))
+	discoveryPtr, err := jutil.JLongField(env, thisObject, "nativeDiscovery")
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+
+	ds := *(*discovery.T)(jutil.NativePtr(jutil.Object(unsafe.Pointer(uintptr(discoveryPtr)))))
+
+	triggerPtr, err := jutil.JLongField(env, thisObject, "nativeTrigger")
+
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+
+	trigger := (*idiscovery.Trigger)(jutil.NativePtr(jutil.Object(unsafe.Pointer(uintptr(triggerPtr)))))
+
+	done, err := ds.Advertise(ctx, service, permsSlice)
+
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	cb := jutil.Object(uintptr(unsafe.Pointer(jCallback)))
+	jutil.NewGlobalRef(env, cb)
+	trigger.Add(func() {
+		env, freeFunc := jutil.GetEnv()
+		defer freeFunc()
+		// TODO(bjornick): What should we do on errors?
+		if err := jutil.CallVoidMethod(env, cb, "done", []jutil.Sign{}); err != nil {
+			jutil.JThrowV(env, err)
+		}
+		jutil.DeleteGlobalRef(env, cb)
+	}, done)
+}
+
+//export Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_scan
+func Java_io_v_impl_google_lib_discovery_VDiscoveryImpl_scan(jenv *C.JNIEnv, this C.jobject, jContext C.jobject, jQuery C.jstring, jCallback C.jobject) {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	ctx, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
+	if err != nil {
+		return
+	}
+	query := jutil.GoString(env, jutil.Object(uintptr(unsafe.Pointer(jQuery))))
+
+	thisObject := jutil.Object(uintptr(unsafe.Pointer(this)))
+	discoveryPtr, err := jutil.JLongField(env, thisObject, "nativeDiscovery")
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	ds := *(*discovery.T)(jutil.NativePtr(jutil.Object(unsafe.Pointer(uintptr(discoveryPtr)))))
+
+	updates, err := ds.Scan(ctx, query)
+
+	jutil.NewGlobalRef(env, jutil.Object(uintptr(unsafe.Pointer(jCallback))))
+	if err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	cb := jutil.Object(uintptr(unsafe.Pointer(jCallback)))
+	jutil.NewGlobalRef(env, cb)
+	go func() {
+		env, freeFunc := jutil.GetEnv()
+		defer freeFunc()
+		for v := range updates {
+			jUpdate, err := jutil.JVomCopy(env, v, jUpdateClass)
+			if err != nil {
+				ctx.Errorf("Failed to convert update: %v", err)
+				continue
+			}
+			err = jutil.CallVoidMethod(env, cb, "handleUpdate", []jutil.Sign{updateSign}, jUpdate)
+			if err != nil {
+				ctx.Errorf("Failed to call handler: %v", err)
+				continue
+			}
+		}
+		jutil.DeleteGlobalRef(env, cb)
+	}()
+}
+
+
diff --git a/impl/google/rt/jni.go b/impl/google/rt/jni.go
index c5239ba..71f2369 100644
--- a/impl/google/rt/jni.go
+++ b/impl/google/rt/jni.go
@@ -11,7 +11,8 @@
 
 	"v.io/v23"
 	"v.io/v23/context"
-	_ "v.io/x/ref/runtime/factories/roaming"
+
+	idiscovery "v.io/x/ref/lib/discovery"
 
 	jns "v.io/x/jni/impl/google/namespace"
 	jrpc "v.io/x/jni/impl/google/rpc"
@@ -28,6 +29,8 @@
 	serverSign  = jutil.ClassSign("io.v.v23.rpc.Server")
 
 	jVRuntimeImplClass jutil.Class
+
+	jVDiscoveryImplClass jutil.Class
 )
 
 // Init initializes the JNI code with the given Java environment.  This method
@@ -39,6 +42,12 @@
 	if err != nil {
 		return err
 	}
+
+	jVDiscoveryImplClass, err = jutil.JFindClass(env, "io/v/impl/google/lib/discovery/VDiscoveryImpl")
+	if err != nil {
+		return err
+	}
+
 	return nil
 }
 
@@ -272,3 +281,25 @@
 	}
 	return C.jobject(unsafe.Pointer(jSpec))
 }
+
+//export Java_io_v_impl_google_rt_VRuntimeImpl_nativeGetDiscovery
+func Java_io_v_impl_google_rt_VRuntimeImpl_nativeGetDiscovery(jenv *C.JNIEnv, jclass C.jclass, jContext C.jobject) C.jobject {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	ctx, err := jcontext.GoContext(env, jutil.Object(uintptr(unsafe.Pointer(jContext))))
+	if err != nil {
+		jutil.JThrowV(env, err);
+		return C.jobject(unsafe.Pointer(jutil.NullObject))
+	}
+	d := v23.GetDiscovery(ctx)
+	ptrD := &d
+	jutil.GoRef(ptrD)
+	trigger := idiscovery.NewTrigger()
+	jutil.GoRef(trigger)
+
+	jDiscovery, err := jutil.NewObject(env, jVDiscoveryImplClass, []jutil.Sign{jutil.LongSign, jutil.LongSign}, int64(jutil.PtrValue(ptrD)), int64(jutil.PtrValue(trigger)))
+	if err != nil {
+		jutil.JThrowV(env, err);
+		return C.jobject(unsafe.Pointer(jutil.NullObject))
+	}
+	return C.jobject(unsafe.Pointer(jDiscovery))
+}
diff --git a/jni.go b/jni.go
index fc311e0..9168811 100644
--- a/jni.go
+++ b/jni.go
@@ -2,19 +2,16 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build java android
+// +build java
 
 package jni
 
 import (
-	"os"
 	"unsafe"
 
-	"v.io/x/lib/vlog"
-
-	jgoogle "v.io/x/jni/impl/google"
 	jutil "v.io/x/jni/util"
-	jv23 "v.io/x/jni/v23"
+
+	_ "v.io/x/ref/runtime/factories/roaming"
 )
 
 // #include "jni.h"
@@ -23,29 +20,8 @@
 //export Java_io_v_v23_V_nativeInit
 func Java_io_v_v23_V_nativeInit(jenv *C.JNIEnv, jVRuntimeClass C.jclass) {
 	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
-	// Ignore all args except for the first one.
-	// NOTE(spetrovic): in the future, we could accept all arguments that are
-	// actually defined in Go.  We'd have to manually check.
-	if len(os.Args) > 1 {
-		os.Args = os.Args[:1]
-	}
-	// Send all logging to stderr, so that the output is visible in android.
-	// Note that if this flag is removed, the process will likely crash on
-	// android as android requires that all logs are written into the app's
-	// local directory.
-	vlog.Log.Configure(vlog.LogToStderr(true))
-	if err := jutil.Init(env); err != nil {
-		jutil.JThrowV(env, err)
-		return
-	}
-	if err := jv23.Init(env); err != nil {
-		jutil.JThrowV(env, err)
-		return
-	}
-	if err := jgoogle.Init(env); err != nil {
-		jutil.JThrowV(env, err)
-		return
-	}
+	setupUtil(env)
+	setupV23(env)
 }
 
 func main() {
diff --git a/jni_android.go b/jni_android.go
new file mode 100644
index 0000000..a6e269d
--- /dev/null
+++ b/jni_android.go
@@ -0,0 +1,43 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build android
+
+package jni
+import (
+	jutil "v.io/x/jni/util"
+	"v.io/x/jni/libs/discovery"
+
+	_ "v.io/x/ref/runtime/factories/android"
+	"v.io/x/ref/lib/discovery/factory"
+)
+
+// #include "jni.h"
+import "C"
+
+var (
+	// Global reference for io.v.android.libs.discovery.ble.BlePlugin
+	jBlePlugin jutil.Class
+	// Global reference for io.v.v23.context.CancelableVContext class.
+	jCancelableVContextClass jutil.Class
+	// Global reference for java.jutil.concurrent.CountDownLatch class.
+	jCountDownLatchClass jutil.Class
+)
+
+//export Java_io_v_v23_V_nativeInit
+func Java_io_v_v23_V_nativeInit(jenv *C.JNIEnv, jVRuntimeClass C.jclass, androidCtx C.jobject) {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	setupUtil(env)
+	err := discovery.Init(env)
+	if err != nil {
+		return
+	}
+
+	factory.SetAndroidBleCreator(discovery.NewBleCreator(env, androidCtx))
+
+	setupV23(env)
+}
+
+func main() {
+}
diff --git a/libs/discovery/jni.go b/libs/discovery/jni.go
new file mode 100644
index 0000000..e25e2d6
--- /dev/null
+++ b/libs/discovery/jni.go
@@ -0,0 +1,71 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build android
+
+package discovery
+
+import (
+	"fmt"
+	"unsafe"
+
+	jutil "v.io/x/jni/util"
+	"v.io/x/ref/lib/discovery"
+)
+// #include "jni.h"
+import "C"
+var (
+// Global reference for io.v.android.libs.discovery.ble.BlePlugin
+	jBlePluginClass jutil.Class
+	// Global reference for io.v.x.ref.lib.discovery.Advertisement
+	jAdvertisementClass jutil.Class
+	// Global reference for java.util.UUID
+	jUUIdClass jutil.Class
+	// Global reference for io.v.android.libs.discovery.ble.NativeScanHandler
+	jNativeScanHandlerClass jutil.Class
+)
+
+func Init(env jutil.Env) error {
+	var err error
+	jUUIDClass, err = jutil.JFindClass(env, "java/util/UUID")
+	if err != nil {
+		return err
+	}
+
+	jNativeScanHandlerClass, err = jutil.JFindClass(env, "io/v/android/libs/discovery/ble/NativeScanHandler")
+	if err != nil {
+		return err
+	}
+
+	jBlePluginClass, err = jutil.JFindClass(env, "io/v/android/libs/discovery/ble/BlePlugin")
+	if err != nil {
+		return err
+	}
+
+	jAdvertisementClass, err = jutil.JFindClass(env, "io/v/x/ref/lib/discovery/Advertisement")
+	return err
+}
+
+//export Java_io_android_libs_discovery_ble_NativeScanHandler_nativeHandleUpdate
+func Java_io_android_libs_discovery_ble_NativeScanHandler_nativeHandleUpdate(jenv *C.JNIEnv, _ C.jobject, jAdv C.jobject, goPtr C.jlong)  {
+	env := jutil.Env(uintptr(unsafe.Pointer(jenv)))
+	c, ok := (*(*chan <- discovery.Advertisement)(jutil.NativePtr(goPtr)))
+
+	if !ok {
+		jutil.JThrow(env, fmt.Errorf("Internal error"))
+		return
+	}
+	advObject := jutil.Object(uintptr(unsafe.Pointer(jAdv)))
+	var adv discovery.Advertisement
+	if err := jutil.GoVomCopy(env, advObject, jAdvertisementClass, &adv); err != nil {
+		jutil.JThrow(env, err)
+		return
+	}
+	ch <-advObject
+}
+
+//export Java_io_android_libs_discovery_ble_NativeScanHandler_nativeCleanup
+func Java_io_android_libs_discovery_ble_NativeScanHandler_nativeCleanup(jenv *C.JNIEnv, _ C.jobject, goPtr c.jlong) {
+	jutil.GoUnref(jutil.NativePtr(goPtr))
+}
diff --git a/libs/discovery/plugin.go b/libs/discovery/plugin.go
new file mode 100644
index 0000000..e7e2df7
--- /dev/null
+++ b/libs/discovery/plugin.go
@@ -0,0 +1,143 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build android
+
+package discovery
+import (
+	"bytes"
+	"runtime"
+	"unsafe"
+
+	"v.io/v23/context"
+
+	"v.io/x/ref/lib/discovery"
+	"v.io/x/ref/runtime/factories/android"
+
+	jcontext "v.io/x/jni/v23/context"
+	jutil "v.io/x/jni/util"
+
+)
+
+// #include "jni.h"
+import "C"
+
+var (
+	androidContextSign = jutil.ClassSign("android.content.Context")
+	contextSign = jutil.ClassSign("io.v.v23.Context")
+	advertisementSign = jutil.ClassSign("io.v.x.ref.lib.discovery.Advertisement")
+	uuidSign = jutil.ClassSign("java.util.UUID")
+	scanHandlerSign = jutil.ClassSign("io.v.impl.google.lib.discovery.ScanHandler")
+)
+
+
+func NewPluginCreator(env jutil.Env, context C.jobject) func(string) (discovery.Plugin, error) {
+	// Reference Java VPrincipal; it will be de-referenced when the Go Principal
+	// created below is garbage-collected (through the finalizer callback we
+	// setup just below).
+	jContext = jutil.NewGlobalRef(env, context)
+	return func(host string) (discovery.Plugin, error) {
+		env, freeFunc := jutil.GetEnv()
+		defer freeFunc()
+		jPlugin, err := jutil.NewObject(env, jBlePluginClass, []jutil.Sign{androidContextSign}, context)
+
+		jutil.DeleteGlobalRef(env, jContext)
+		if err != nil {
+			return nil, err
+		}
+		jplugin = jutil.NewGlobalRef(env, jPlugin)
+		p := &plugin{
+			trigger: discovery.NewTrigger(),
+			jPlugin: jPlugin,
+		}
+		runtime.SetFinalizer(p, func(p *plugin) {
+			env, freeFunc := jutil.GetEnv()
+			defer freeFunc()
+			jutil.DeleteGlobalRef(env, p.jPlugin)
+		})
+		return p, nil
+	}
+
+}
+
+type plugin struct {
+	trigger *discovery.Trigger
+	jPlugin C.jobject
+}
+
+// Plugin is the basic interface for a plugin to discovery service.
+// All implementation should be goroutine-safe.
+type Plugin interface {
+	// Advertise advertises the advertisement. Advertising will continue until
+	// the context is canceled or exceeds its deadline. done should be called
+	// once when advertising is done or canceled.
+	Advertise(ctx *context.T, ad Advertisement, done func()) error
+
+	// Scan scans services that match the service uuid and returns scanned
+	// advertisements to the channel. A zero-value service uuid means any service.
+	// Scanning will continue until the context is canceled or exceeds its
+	// deadline. done should be called once when scanning is done or canceled.
+	//
+	// TODO(jhahn): Pass a filter on service attributes.
+	Scan(ctx *context.T, serviceUuid Uuid, ch chan<- Advertisement, done func()) error
+}
+func (p *plugin) Advertise(ctx *context.T, ad discovery.Advertisement, done func()) error {
+	env, freeFunc := jutil.GetEnv()
+	defer freeFunc()
+	jContext, err := jcontext.NewJavaContext(env, ctx)
+	if err != nil {
+		return err
+	}
+	jAdv, err := jutil.JVomCopy(env, ad, jAdvertisementClass)
+	if err != nil {
+		return err
+	}
+
+	err := jutil.CallVoidMethod(env, p.jPlugin, "addAdvertisesment", []jutil.Sign{contextSign, advertisementSign}, jContext, jAdv)
+
+	p.trigger.Add(done, ctx.Done())
+
+	return err
+}
+
+func (p *plugin) Scan(ctx *context.T, serviceUuid discovery.Uuid, ch chan<- discovery.Advertisement, done func()) error {
+	env, freeFunc := jutil.GetEnv()
+	defer freeFunc()
+	jContext, err := jcontext.NewJavaContext(env, ctx)
+	if err != nil {
+		return err
+	}
+
+	jUuid, err := convertToJavaUUID(env, serviceUuid)
+
+	if err != nil {
+		return err
+	}
+
+
+	chPtr := &ch
+	jutil.GoRef(chPtr)
+	jNativeScanHandler, err := jutil.NewObject(env, jNativeScanHandlerClass, []jutil.Sign{jutil.LongSign}, int64(jutil.PtrValue(chPtr)))
+	if err != nil {
+		return err
+	}
+
+	err = jutil.CallVoidMethod(env, p.jPlugin, "addScanner", []jutil.Sign{contextSign, uuidSign, scanHandlerSign},
+		jContext, jUuid, jNativeScanHandler)
+
+	if err != nil {
+		return err
+	}
+	p.trigger.Add(done, ctx.Done())
+	return nil
+}
+
+
+func convertToJavaUUID(env jutil.Env, uuid discovery.Uuid) (C.jobject, error) {
+	buf := bytes.NewBuffer(uuid)
+	var high, low int64
+	binary.Read(buf, binary.BigEndian, &high)
+	binary.Read(buf, binary.BigEndian, &low)
+	jUUID, err := jutil.NewObject(env, jUUIDClass, []jutil.Sign{jutil.LongSign, jutil.LongSign}, high, low)
+}
diff --git a/shared.go b/shared.go
new file mode 100644
index 0000000..7832b4a
--- /dev/null
+++ b/shared.go
@@ -0,0 +1,49 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build java android
+
+package jni
+
+import (
+	"os"
+
+	"v.io/x/lib/vlog"
+
+	jgoogle "v.io/x/jni/impl/google"
+	jutil "v.io/x/jni/util"
+	jv23 "v.io/x/jni/v23"
+)
+
+// #include "jni.h"
+import "C"
+
+func setupUtil(env jutil.Env) {
+	// Ignore all args except for the first one.
+	// NOTE(spetrovic): in the future, we could accept all arguments that are
+	// actually defined in Go.  We'd have to manually check.
+	if len(os.Args) > 1 {
+		os.Args = os.Args[:1]
+	}
+	// Send all logging to stderr, so that the output is visible in android.
+	// Note that if this flag is removed, the process will likely crash on
+	// android as android requires that all logs are written into the app's
+	// local directory.
+	vlog.Log.Configure(vlog.LogToStderr(true))
+	if err := jutil.Init(env); err != nil {
+		jutil.JThrowV(env, err)
+	}
+}
+
+func setupV23(env jutil.Env) {
+	if err := jv23.Init(env); err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+	if err := jgoogle.Init(env); err != nil {
+		jutil.JThrowV(env, err)
+		return
+	}
+}
+