Skip to content

Commit

Permalink
object and array conversion examples
Browse files Browse the repository at this point in the history
  • Loading branch information
freezer333 committed Apr 2, 2016
1 parent a701484 commit 35fa8e7
Show file tree
Hide file tree
Showing 12 changed files with 825 additions and 108 deletions.
43 changes: 43 additions & 0 deletions conversions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,48 @@ describe('loose conversions', function() {
});

});

describe('pass_object()', function () {
/* pass_object accepts any object and extracts numeric x/y properties. It
then constructs a new object with the sum and product of x/y. It
demonstrates how properties can be handled with objects in V8.
*/

it('should fully compute properties given object with required properties', function () {
assert.equal(13, loose.pass_object({x:3, y:10}).sum);
assert.equal(30, loose.pass_object({x:3, y:10}).product);
});
it('should set sum and product to NaN given object without x or y', function () {
assert(13, isNaN(loose.pass_object({y:10}).sum));
assert(30, isNaN(loose.pass_object({x:3}).product));
assert(isNaN(loose.pass_object({x:3, y:"hello"}).product));
});
it('should fully compute properties given object with required properties - even when not pure numerics', function () {
assert.equal(4, loose.pass_object({x:3, y:true}).sum);
assert.equal(30, loose.pass_object({x:3, y:"10"}).product);
assert.equal(0, loose.pass_object({x:3, y:null}).product);
});
});



describe('pass_array()', function () {
/* pass_array accepts an array (input) and assumes it has 3 numeric elements at index 0-2.
It also looks for an additional property called "not_index".
It contstructs a new array consisting of [input[0]+1, input.not_index, input[2]+1].
No one said these functions should be sensible ;)
*/

it('should fully compute given expected array', function () {
var a = [4, 7, 9];
a.not_index = "hello";
assert.deepEqual([5, "hello", 10], loose.pass_array(a));
});
it('should return array with undefined values given incomplete input', function() {
assert.deepEqual([2, undefined, 4], loose.pass_array([1, 2, 3]));
assert.deepEqual([2, undefined, undefined], loose.pass_array([1, 2]));
assert.deepEqual([undefined, undefined, undefined], loose.pass_array([]));
});
});
});

88 changes: 53 additions & 35 deletions conversions/loose/build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ all_deps :=


CC.target ?= $(CC)
CFLAGS.target ?= $(CFLAGS)
CFLAGS.target ?= $(CPPFLAGS) $(CFLAGS)
CXX.target ?= $(CXX)
CXXFLAGS.target ?= $(CXXFLAGS)
CXXFLAGS.target ?= $(CPPFLAGS) $(CXXFLAGS)
LINK.target ?= $(LINK)
LDFLAGS.target ?= $(LDFLAGS)
AR.target ?= $(AR)
Expand All @@ -54,9 +54,9 @@ LINK ?= $(CXX.target)
# TODO(evan): move all cross-compilation logic to gyp-time so we don't need
# to replicate this environment fallback in make as well.
CC.host ?= gcc
CFLAGS.host ?=
CFLAGS.host ?= $(CPPFLAGS_host) $(CFLAGS_host)
CXX.host ?= g++
CXXFLAGS.host ?=
CXXFLAGS.host ?= $(CPPFLAGS_host) $(CXXFLAGS_host)
LINK.host ?= $(CXX.host)
LDFLAGS.host ?=
AR.host ?= ar
Expand Down Expand Up @@ -126,46 +126,52 @@ cmd_cc = $(CC.$(TOOLSET)) $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c -o $
quiet_cmd_cxx = CXX($(TOOLSET)) $@
cmd_cxx = $(CXX.$(TOOLSET)) $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<

quiet_cmd_objc = CXX($(TOOLSET)) $@
cmd_objc = $(CC.$(TOOLSET)) $(GYP_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<

quiet_cmd_objcxx = CXX($(TOOLSET)) $@
cmd_objcxx = $(CXX.$(TOOLSET)) $(GYP_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<

# Commands for precompiled header files.
quiet_cmd_pch_c = CXX($(TOOLSET)) $@
cmd_pch_c = $(CC.$(TOOLSET)) $(GYP_PCH_CFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_pch_cc = CXX($(TOOLSET)) $@
cmd_pch_cc = $(CC.$(TOOLSET)) $(GYP_PCH_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
quiet_cmd_pch_m = CXX($(TOOLSET)) $@
cmd_pch_m = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
quiet_cmd_pch_mm = CXX($(TOOLSET)) $@
cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<

# gyp-mac-tool is written next to the root Makefile by gyp.
# Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd
# already.
quiet_cmd_mac_tool = MACTOOL $(4) $<
cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@"

quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@
cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4)

quiet_cmd_infoplist = INFOPLIST $@
cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@"

quiet_cmd_touch = TOUCH $@
cmd_touch = touch $@

quiet_cmd_copy = COPY $@
# send stderr to /dev/null to ignore messages when linking directories.
cmd_copy = rm -rf "$@" && cp -af "$<" "$@"

quiet_cmd_alink = AR($(TOOLSET)) $@
cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^)
quiet_cmd_alink = LIBTOOL-STATIC $@
cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool $(GYP_LIBTOOLFLAGS) -static -o $@ $(filter %.o,$^)

quiet_cmd_alink_thin = AR($(TOOLSET)) $@
cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^)

# Due to circular dependencies between libraries :(, we wrap the
# special "figure out circular dependencies" flags around the entire
# input list during linking.
quiet_cmd_link = LINK($(TOOLSET)) $@
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ -Wl,--start-group $(LD_INPUTS) -Wl,--end-group $(LIBS)

# We support two kinds of shared objects (.so):
# 1) shared_library, which is just bundling together many dependent libraries
# into a link line.
# 2) loadable_module, which is generating a module intended for dlopen().
#
# They differ only slightly:
# In the former case, we want to package all dependent code into the .so.
# In the latter case, we want to package just the API exposed by the
# outermost module.
# This means shared_library uses --whole-archive, while loadable_module doesn't.
# (Note that --whole-archive is incompatible with the --start-group used in
# normal linking.)

# Other shared-object link notes:
# - Set SONAME to the library filename so our binaries don't reference
# the local, absolute paths used on the link command-line.
cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)

quiet_cmd_solink = SOLINK($(TOOLSET)) $@
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ -Wl,--whole-archive $(LD_INPUTS) -Wl,--no-whole-archive $(LIBS)
cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)

quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ -Wl,--start-group $(filter-out FORCE_DO_CMD, $^) -Wl,--end-group $(LIBS)
cmd_solink_module = $(LINK.$(TOOLSET)) -bundle $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)


# Define an escape_quotes function to escape single quotes.
Expand Down Expand Up @@ -230,7 +236,7 @@ define do_cmd
$(if $(or $(command_changed),$(prereq_changed)),
@$(call exact_echo, $($(quiet)cmd_$(1)))
@mkdir -p "$(call dirx,$@)" "$(dir $(depfile))"
$(if $(findstring flock,$(word 1,$(cmd_$1))),
$(if $(findstring flock,$(word 2,$(cmd_$1))),
@$(cmd_$(1))
@echo " $(quiet_cmd_$(1)): Finished",
@$(cmd_$(1))
Expand Down Expand Up @@ -268,6 +274,10 @@ $(obj).$(TOOLSET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cxx FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.m FORCE_DO_CMD
@$(call do_cmd,objc,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.mm FORCE_DO_CMD
@$(call do_cmd,objcxx,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.S FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(srcdir)/%.s FORCE_DO_CMD
Expand All @@ -282,6 +292,10 @@ $(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cxx FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.m FORCE_DO_CMD
@$(call do_cmd,objc,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.mm FORCE_DO_CMD
@$(call do_cmd,objcxx,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.S FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.s FORCE_DO_CMD
Expand All @@ -295,6 +309,10 @@ $(obj).$(TOOLSET)/%.o: $(obj)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.cxx FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.m FORCE_DO_CMD
@$(call do_cmd,objc,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.mm FORCE_DO_CMD
@$(call do_cmd,objcxx,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.S FORCE_DO_CMD
@$(call do_cmd,cc,1)
$(obj).$(TOOLSET)/%.o: $(obj)/%.s FORCE_DO_CMD
Expand All @@ -308,8 +326,8 @@ ifeq ($(strip $(foreach prefix,$(NO_LOAD),\
endif

quiet_cmd_regen_makefile = ACTION Regenerating $@
cmd_regen_makefile = cd $(srcdir); /home/sfrees/.node/lib/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "--toplevel-dir=." -I/home/sfrees/projects/nodecpp-demo/conversions/loose/build/config.gypi -I/home/sfrees/.node/lib/node_modules/node-gyp/addon.gypi -I/home/sfrees/.node-gyp/4.0.0/common.gypi "--depth=." "-Goutput_dir=." "--generator-output=build" "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/home/sfrees/.node-gyp/4.0.0" "-Dnode_gyp_dir=/home/sfrees/.node/lib/node_modules/node-gyp" "-Dmodule_root_dir=/home/sfrees/projects/nodecpp-demo/conversions/loose" binding.gyp
Makefile: $(srcdir)/../../../../.node-gyp/4.0.0/common.gypi $(srcdir)/../../../../.node/lib/node_modules/node-gyp/addon.gypi $(srcdir)/build/config.gypi $(srcdir)/binding.gyp
cmd_regen_makefile = cd $(srcdir); /usr/local/lib/node_modules/node-gyp/gyp/gyp_main.py -fmake --ignore-environment "--toplevel-dir=." -I/Users/sfrees/projects/nodecpp-demo/conversions/loose/build/config.gypi -I/usr/local/lib/node_modules/node-gyp/addon.gypi -I/Users/sfrees/.node-gyp/0.12.5/common.gypi "--depth=." "-Goutput_dir=." "--generator-output=build" "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/sfrees/.node-gyp/0.12.5" "-Dnode_gyp_dir=/usr/local/lib/node_modules/node-gyp" "-Dnode_lib_file=node.lib" "-Dmodule_root_dir=/Users/sfrees/projects/nodecpp-demo/conversions/loose" binding.gyp
Makefile: $(srcdir)/../../../../../../usr/local/lib/node_modules/node-gyp/addon.gypi $(srcdir)/../../../../.node-gyp/0.12.5/common.gypi $(srcdir)/build/config.gypi $(srcdir)/binding.gyp
$(call do_cmd,regen_makefile)

# "all" is a concatenation of the "all" targets from all the included
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cmd_Release/loose_type_demo.node := rm -rf "Release/loose_type_demo.node" && cp -af "Release/obj.target/loose_type_demo.node" "Release/loose_type_demo.node"
cmd_Release/loose_type_demo.node := c++ -bundle -undefined dynamic_lookup -Wl,-search_paths_first -mmacosx-version-min=10.5 -arch x86_64 -L./Release -o Release/loose_type_demo.node Release/obj.target/loose_type_demo/loose_type_demo.o
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
cmd_Release/obj.target/loose_type_demo/loose_type_demo.o := g++ '-DNODE_GYP_MODULE_NAME=loose_type_demo' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' -I/home/sfrees/.node-gyp/4.0.0/src -I/home/sfrees/.node-gyp/4.0.0/deps/uv/include -I/home/sfrees/.node-gyp/4.0.0/deps/v8/include -fPIC -pthread -Wall -Wextra -Wno-unused-parameter -m64 -O3 -ffunction-sections -fdata-sections -fno-omit-frame-pointer -fno-rtti -fno-exceptions -std=gnu++0x -MMD -MF ./Release/.deps/Release/obj.target/loose_type_demo/loose_type_demo.o.d.raw -c -o Release/obj.target/loose_type_demo/loose_type_demo.o ../loose_type_demo.cpp
cmd_Release/obj.target/loose_type_demo/loose_type_demo.o := c++ '-DNODE_GYP_MODULE_NAME=loose_type_demo' '-D_DARWIN_USE_64_BIT_INODE=1' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-DBUILDING_NODE_EXTENSION' -I/Users/sfrees/.node-gyp/0.12.5/include/node -I/Users/sfrees/.node-gyp/0.12.5/src -I/Users/sfrees/.node-gyp/0.12.5/deps/uv/include -I/Users/sfrees/.node-gyp/0.12.5/deps/v8/include -Os -gdwarf-2 -mmacosx-version-min=10.5 -arch x86_64 -Wall -Wendif-labels -W -Wno-unused-parameter -fno-rtti -fno-exceptions -fno-threadsafe-statics -fno-strict-aliasing -MMD -MF ./Release/.deps/Release/obj.target/loose_type_demo/loose_type_demo.o.d.raw -c -o Release/obj.target/loose_type_demo/loose_type_demo.o ../loose_type_demo.cpp
Release/obj.target/loose_type_demo/loose_type_demo.o: \
../loose_type_demo.cpp /home/sfrees/.node-gyp/4.0.0/src/node.h \
/home/sfrees/.node-gyp/4.0.0/deps/v8/include/v8.h \
/home/sfrees/.node-gyp/4.0.0/deps/v8/include/v8-version.h \
/home/sfrees/.node-gyp/4.0.0/deps/v8/include/v8config.h \
/home/sfrees/.node-gyp/4.0.0/src/node_version.h
../loose_type_demo.cpp /Users/sfrees/.node-gyp/0.12.5/src/node.h \
/Users/sfrees/.node-gyp/0.12.5/deps/v8/include/v8.h \
/Users/sfrees/.node-gyp/0.12.5/deps/v8/include/v8stdint.h \
/Users/sfrees/.node-gyp/0.12.5/deps/v8/include/v8config.h \
/Users/sfrees/.node-gyp/0.12.5/src/node_version.h
../loose_type_demo.cpp:
/home/sfrees/.node-gyp/4.0.0/src/node.h:
/home/sfrees/.node-gyp/4.0.0/deps/v8/include/v8.h:
/home/sfrees/.node-gyp/4.0.0/deps/v8/include/v8-version.h:
/home/sfrees/.node-gyp/4.0.0/deps/v8/include/v8config.h:
/home/sfrees/.node-gyp/4.0.0/src/node_version.h:
/Users/sfrees/.node-gyp/0.12.5/src/node.h:
/Users/sfrees/.node-gyp/0.12.5/deps/v8/include/v8.h:
/Users/sfrees/.node-gyp/0.12.5/deps/v8/include/v8stdint.h:
/Users/sfrees/.node-gyp/0.12.5/deps/v8/include/v8config.h:
/Users/sfrees/.node-gyp/0.12.5/src/node_version.h:
Empty file.
Binary file modified conversions/loose/build/Release/loose_type_demo.node
Binary file not shown.
Binary file not shown.
29 changes: 14 additions & 15 deletions conversions/loose/build/config.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,44 @@
"libraries": []
},
"variables": {
"asan": 0,
"gas_version": "2.23",
"clang": 1,
"host_arch": "x64",
"icu_data_file": "icudt55l.dat",
"icu_data_in": "../../deps/icu/source/data/in/icudt55l.dat",
"icu_data_file": "icudt54l.dat",
"icu_data_in": "../../deps/icu/source/data/in/icudt54l.dat",
"icu_endianness": "l",
"icu_gyp_path": "tools/icu/icu-generic.gyp",
"icu_locales": "en,root",
"icu_path": "./deps/icu",
"icu_small": "true",
"icu_ver_major": "55",
"node_byteorder": "little",
"icu_ver_major": "54",
"node_install_npm": "true",
"node_prefix": "/",
"node_release_urlbase": "https://nodejs.org/download/release/",
"node_prefix": "",
"node_shared_cares": "false",
"node_shared_http_parser": "false",
"node_shared_libuv": "false",
"node_shared_openssl": "false",
"node_shared_v8": "false",
"node_shared_zlib": "false",
"node_tag": "",
"node_use_dtrace": "false",
"node_use_dtrace": "true",
"node_use_etw": "false",
"node_use_lttng": "false",
"node_use_mdb": "false",
"node_use_openssl": "true",
"node_use_perfctr": "false",
"openssl_fips": "",
"openssl_no_asm": 0,
"python": "/home/iojs/bin/python",
"python": "/usr/bin/python",
"target_arch": "x64",
"uv_library": "static_library",
"uv_parent_path": "/deps/uv/",
"uv_use_dtrace": "false",
"uv_use_dtrace": "true",
"v8_enable_gdbjit": 0,
"v8_enable_i18n_support": 1,
"v8_no_strict_aliasing": 1,
"v8_optimized_debug": 0,
"v8_random_seed": 0,
"v8_use_snapshot": 1,
"v8_use_snapshot": "false",
"want_separate_host_toolset": 0,
"nodedir": "/home/sfrees/.node-gyp/4.0.0",
"nodedir": "/Users/sfrees/.node-gyp/0.12.5",
"copy_dev_lib": "true",
"standalone_static_library": 1
}
Expand Down
Loading

0 comments on commit 35fa8e7

Please sign in to comment.