Skip to content

Commit

Permalink
Merge pull request #2 from cxw42/expose-class
Browse files Browse the repository at this point in the history
First version of class binding
  • Loading branch information
cxw42 authored Mar 30, 2021
2 parents 7284b5a + 2765b38 commit fef588f
Show file tree
Hide file tree
Showing 15 changed files with 1,807 additions and 49 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Generated by configure
Makefile
libwren*.pc
/src/vm.vala
/src/basics.vapi
/src/marshal.vala
/src/trampoline.vala
/src/vm.vala
/src/wrennull.c
/t/common.sh

# Prerequisites
Expand Down
24 changes: 24 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,47 @@ EXTRA_DIST = \
distcheck-hook:
chmod -R u+w $(distdir)

# --- Code hygiene -------------------------------------------

phony += prettyprint
prettyprint:
-$(AM_V_GEN)uncrustify -l VALA -c $(top_srcdir)/.uncrustifyrc --replace \
src/basics.vapi.in \
src/marshal.vala.in \
src/trampoline.vala.in \
src/vm.vala.in \
src/wrennull.c.in \
t/000-sanity-t.vala \
t/105-tramp-t.vala \
t/100-vmv-t.vala \
t/110-hello-world-s.vala \
t/120-read-var-from-wren-t.vala \
t/130-call-wren-t.vala \
t/140-roundtrip-data-t.vala \
t/150-marshal-t.vala \
t/200-expose-class-t.vala \
$(EOL)

phony += p
p: prettyprint
-ctags -R

phony += cleanwren
cleanwren:
-cd wren-pkg/wren && git clean -dfx

# --- Testing ------------------------------------------------

# Shortcuts for tests
phony += test
test: check

phony += build-tests
build-tests: all
+$(MAKE) -C t $@

# --- Coverage -----------------------------------------------

# Used by coverage.sh --- remove the existing code-coverage data.
remove-code-coverage-data:
-rm -rf "$(CODE_COVERAGE_OUTPUT_FILE)" "$(CODE_COVERAGE_OUTPUT_FILE).tmp" "$(CODE_COVERAGE_OUTPUT_DIRECTORY)"
Expand Down
9 changes: 7 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,18 @@ AC_CONFIG_FILES([
Makefile
doc/Makefile
src/Makefile
src/basics.vapi
src/vm.vala
t/Makefile
t/common.sh
wren-pkg/Makefile
])

dnl Source files: make them RO
AC_CONFIG_FILES([src/basics.vapi], [chmod a-w src/basics.vapi])
AC_CONFIG_FILES([src/marshal.vala], [chmod a-w src/marshal.vala])
AC_CONFIG_FILES([src/trampoline.vala], [chmod a-w src/trampoline.vala])
AC_CONFIG_FILES([src/vm.vala], [chmod a-w src/vm.vala])
AC_CONFIG_FILES([src/wrennull.c], [chmod a-w src/wrennull.c])

dnl pkg-config files: name them after the Wren version
AC_CONFIG_FILES([wren-pkg/libwren-"${APIVER}".pc:wren-pkg/libwren.pc.in], [], [APIVER=$APIVER])
AC_CONFIG_FILES([src/libwren-vala-"${APIVER}".pc:src/libwren-vala.pc.in], [], [APIVER=$APIVER])
Expand Down
5 changes: 4 additions & 1 deletion doc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ valadoc:
$(AM_V_GEN)valadoc -o valadoc -b $(top_srcdir)/src --verbose --force \
--package-name='@PACKAGE_TARNAME@' \
--package-version='@PACKAGE_VERSION@' \
$(top_srcdir)/src/basics.vapi $(top_srcdir)/src/vm.vala \
$(top_srcdir)/src/basics.vapi \
$(top_srcdir)/src/marshal.vala \
$(top_srcdir)/src/trampoline.vala \
$(top_srcdir)/src/vm.vala \
--doclet=html \
$(MY_VALA_PKGS) \
$(EOL)
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ endif

libwren_vala_la_SOURCES = \
basics.vapi \
marshal.vala \
myconfig.vapi \
trampoline.vala \
vm.vala \
wrennull.c \
$(EOL)

libwren_vala_la_VALAFLAGS = \
Expand Down
5 changes: 4 additions & 1 deletion src/basics.vapi.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Wren {
[CCode(has_target = false)]
public delegate void LoadModuleCompleteFn(VM vm, string name, LoadModuleResult result);

[SimpleType]
public struct LoadModuleResult
{
string source;
Expand Down Expand Up @@ -96,6 +97,8 @@ namespace Wren {
public delegate void ErrorFn(VM vm, ErrorType type, string? module,
int line, string message);

// Has to be a SimpleType so it can be returned by value from functions.
[SimpleType]
public struct ForeignClassMethods
{
ForeignMethodFn allocate;
Expand Down Expand Up @@ -177,7 +180,7 @@ namespace Wren {
public extern int GetSlotCount();
public extern void EnsureSlots(int numSlots);

public extern Type GetSlotType(int slot);
public extern Wren.Type GetSlotType(int slot);
public extern bool GetSlotBool(int slot);
public extern unowned uint8[] GetSlotBytes(int slot);
public extern double GetSlotDouble(int slot);
Expand Down
127 changes: 127 additions & 0 deletions src/marshal.vala.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// @configure_input@

// marshal.vala: Wren<->Vala marshalling
//
// By Christopher White <[email protected]>
// Copyright (c) 2021 Christopher White. All rights reserved.
// SPDX-License-Identifier: MIT

[CCode(cheader_filename="libwren-vala-@[email protected],@WREN_HEADER@")]
namespace Wren {

/**
* Vala type representing the Wren null value.
*
* We use this because you can't create {@link GLib.Value}s of
* {@link GLib.Type.NONE}.
*
* In wrennull.c.
*/
public extern GLib.Type get_null_type();

/**
* Initialize the Wren Vala bindings.
*
* Call this before calling any of the marshalling functions.
* {@link Wren.VMV}'s constructors call this for you.
*
* Idempotent.
*/
public void static_init()
{
if(!static_init_done) {
return;
}

// At the moment, this function is somewhat pointless, since the only
// thing in here is the idempotent initialization of the wren-null type.
// However, leaving the function here means that we won't have to change
// the API if we have to add more initialization steps in the future.

get_null_type();
static_init_done = true;
}
private bool static_init_done = false;

[CCode(cheader_filename="libwren-vala-@[email protected],@WREN_HEADER@")]
namespace Marshal {

public errordomain MarshalError {
/** Slot or other item not found */
ENOENT,
}

/**
* Wrap a single Wren slot in a GValue.
*
* The mapping is:
* * Wren BOOL -> GLib.Type.BOOLEAN
* * Wren NULL -> GLib.Type.NONE
*
* @param vm The vm to read from
* @param slot The slot to grab. It must exist.
*/
public Value to_value_raw(Wren.VM vm, int slot)
{
var ty = vm.GetSlotType(slot);

switch(ty) {
case BOOL:
return vm.GetSlotBool(slot);
case NUM:
return vm.GetSlotDouble(slot);
case FOREIGN:
// TODO
break;
case LIST:
// TODO
break;
case MAP:
// TODO
break;
case NULL:
return Value(get_null_type());
case STRING:
return vm.GetSlotString(slot);
case UNKNOWN:
// TODO
break;
default:
assert_not_reached();
}

return Value(GLib.Type.INVALID);
}

/**
* Wrap Wren slots in a GValue array.
*
* @param vm The vm to read from
* @param first_slot The first slot to grab (default 0)
* @param num_slots How many slots to grab (default -1 == the whole
* slot array)
* @return An array of freshly-created GValues.
*/
public Value[] to_values_raw(Wren.VM vm, int first_slot = 0, int num_slots = -1)
throws MarshalError
{
if(num_slots == -1) {
num_slots = vm.GetSlotCount();
}

if(first_slot + num_slots >= vm.GetSlotCount()) {
throw new MarshalError.ENOENT(
"Slots up to %d requested, but only %d are available".printf(
first_slot + num_slots, vm.GetSlotCount()));
}

Value[] retval = new Value[num_slots];
for(int i=0; i<num_slots; ++i) {
int curr_slot = first_slot + i;
retval[i] = to_value_raw(vm, curr_slot);
}

return retval;
}
}
} // namespace Wren
Loading

0 comments on commit fef588f

Please sign in to comment.