### Install CDT via Debian Package Source: https://github.com/antelopeio/cdt/blob/main/README.md Installs the CDT using a downloaded Debian package. This is the recommended and fastest way to get started with CDT. It requires administrative privileges to install packages system-wide. ```shell wget https://github.com/AntelopeIO/cdt/releases/download/v4.1.0/cdt_4.1.0_amd64.deb sudo apt install ./cdt_4.1.0_amd64.deb ``` -------------------------------- ### Build WABT on Windows using CMake and Visual Studio Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md Configures and builds the WABT project on Windows using CMake and Visual Studio. This example sets up a debug build, specifies an installation prefix, and uses the Visual Studio 2015 generator. Requires CMake and Visual Studio. ```bash mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=DEBUG -DCMAKE_INSTALL_PREFIX=..\bin -G "Visual Studio 14 2015" cmake --build .. --config DEBUG --target install ``` -------------------------------- ### wasm2wat File Conversion Examples Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/help/wasm2wat.txt Provides examples of how to use wasm2wat to convert WebAssembly binary files to text format. These examples illustrate specifying input and output files, and excluding debug names. ```bash # Convert test.wasm to test.wat (standard output to file) $ wasm2wat test.wasm -o test.wat ``` ```bash # Convert test.wasm to test.wat, ignoring debug names $ wasm2wat test.wasm --no-debug-names -o test.wat ``` -------------------------------- ### Install Meta-Refl Library and Components (CMake) Source: https://github.com/antelopeio/cdt/blob/main/libraries/meta_refl/CMakeLists.txt Handles the installation of the meta_refl library and associated files. It installs the library targets, public headers, and package configuration files if BLUEGRASS_ENABLE_INSTALL is ON. ```cmake if(BLUEGRASS_ENABLE_INSTALL) include(GNUInstallDirs) message(STATUS "Installing bluegrass meta_refl ...") install(TARGETS meta_refl EXPORT meta_refl_pkg_config LIBRARY PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT Headers) install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/bluegrass DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT meta_refl_pkg_config DESTINATION "${CMAKE_INSTALL_DATADIR}/meta_refl/cmake" NAMESPACE bluegrass::) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/meta_refl-config-version.cmake DESTINATION "#{CMAKE_INSTALL_DATADIR}/meta_refl/cmake") endif() ``` -------------------------------- ### Manual CDT Installation Source: https://github.com/antelopeio/cdt/blob/main/README.md Installs CDT globally on the system after building. This command should be executed from within the 'build' directory and requires root privileges. ```shell sudo make install ``` -------------------------------- ### WASM Loop Control Flow Example (WAT) Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/brif-multi.txt This example showcases the `loop` instruction in WebAssembly. It shows how to create a loop that executes until a condition is met, returning `i32` and `i64` values upon exit. ```wat (module ;; loop (func loop (result i32 i64) i32.const 0 ;; cond br_if 0 i32.const 1 i64.const 2 end return) ) ``` -------------------------------- ### WASM If Control Flow Example (WAT) Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/brif-multi.txt This example illustrates the `if` instruction in WebAssembly, including an `else` branch. It shows how to conditionally execute code blocks based on a condition and how both branches can return multiple values. ```wat (module ;; if (func i32.const 1 if (result i32 i64) i32.const 1 i64.const 2 i32.const 0 ;; cond br_if 0 else i32.const 3 i64.const 4 i32.const 0 ;; cond br_if 0 end return) ) ``` -------------------------------- ### CMake: Copying and Installing Project Files Source: https://github.com/antelopeio/cdt/blob/main/CMakeLists.txt Copies and installs project-specific files, including import definitions, Python scripts, and licenses. The `configure_file` command is used for template processing, while `install` commands specify the destination directories. This ensures necessary auxiliary files are included in the build output. ```cmake configure_file(${CMAKE_SOURCE_DIR}/imports/cdt.imports.in ${CMAKE_BINARY_DIR}/cdt.imports COPYONLY) install(FILES ${CMAKE_BINARY_DIR}/cdt.imports DESTINATION ${CDT_INSTALL_PREFIX}) configure_file(${CMAKE_SOURCE_DIR}/scripts/ricardeos/ricardeos.py ${CMAKE_BINARY_DIR}/scripts/ricardeos.py COPYONLY) install(FILES ${CMAKE_BINARY_DIR}/scripts/ricardeos.py DESTINATION ${CDT_INSTALL_PREFIX}/scripts) # add licenses configure_file(${CMAKE_SOURCE_DIR}/cdt-llvm/LICENSE.TXT ${CMAKE_BINARY_DIR}/licenses/llvm.license COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/libraries/boost/boost.license ${CMAKE_BINARY_DIR}/licenses/boost.license COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/libraries/meta_refl/LICENSE ${CMAKE_BINARY_DIR}/licenses/meta_refl.license COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/tools/external/wabt/LICENSE ${CMAKE_BINARY_DIR}/licenses/wabt.license COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/tools/jsoncons/LICENSE ${CMAKE_BINARY_DIR}/licenses/jsoncons.license COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/LICENSE ${CMAKE_BINARY_DIR}/licenses/cdt.license COPYONLY) ``` -------------------------------- ### WASM Loop with Parameter Example (WAT) Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/brif-multi.txt This snippet demonstrates a `loop` construct in WebAssembly that accepts a parameter. It shows how to handle the loop parameter, use it for conditional branching, and manage values within the loop. ```wat (module ;; loop w/ param (func i32.const 1 loop (param i32) drop ;; drop loop param i32.const 2 ;; push loop param for br i32.const 0 ;; cond br_if 0 drop ;; drop `i32.const 2` end) ) ``` -------------------------------- ### Generate and Install Debian Package for CDT Source: https://github.com/antelopeio/cdt/blob/main/README.md Generates a Debian package for CDT and then installs it using apt. This process is performed from within the 'packages' directory inside the build folder and requires root privileges for installation. ```shell cd packages bash ./generate_package.sh deb ubuntu-20.04 amd64 sudo apt install ./cdt_*_amd64.deb ``` -------------------------------- ### Install CDT Build Dependencies (Ubuntu) Source: https://github.com/antelopeio/cdt/blob/main/README.md Installs the necessary development tools and libraries required to build CDT from source on Ubuntu. This includes build essentials, compilers, CMake, Git, and Python packages. ```shell apt-get update && apt-get install \ build-essential \ clang \ clang-tidy \ cmake \ git \ libxml2-dev \ opam ocaml-interp \ python3 \ python3-pip \ time ``` -------------------------------- ### WASM Block Control Flow Example (WAT) Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/brif-multi.txt This snippet illustrates the use of the `block` instruction in WebAssembly. It demonstrates how to declare a block that can return multiple values (i32 and i64) and how to conditionally break out of it using `br_if`. ```wat (module ;; block (func block (result i32 i64) i32.const 1 i64.const 2 i32.const 0 ;; cond br_if 0 end return) ) ``` -------------------------------- ### cdt-init Tool Usage and Options Source: https://github.com/antelopeio/cdt/blob/main/docs/03_command-reference/cdt-init.md This section outlines the usage and available options for the cdt-init command-line tool. It details generic options like help and version display, as well as specific options for project generation, including creating a bare project without CMake support and specifying the project path and name. ```bash USAGE: cdt-init [options] OPTIONS: Generic Options: -help - Display available options (-help-hidden for more) -help-list - Display list of available options (-help-list-hidden for more) -version - Display the version of this program cdt-init: generates a smart contract project -bare - produces only a skeleton smart contract without CMake support -path= - directory to place the project -project= - output project name ``` -------------------------------- ### CMake: Include External Projects and Installation Source: https://github.com/antelopeio/cdt/blob/main/CMakeLists.txt Includes definitions for external projects, installation rules, and test configurations. This allows the CMake build system to manage dependencies, define what gets installed, and set up testing infrastructure. It uses `include` directives to pull in modular CMake scripts. ```cmake include(modules/ClangExternalProject.txt) include(modules/ToolsExternalProject.txt) include(modules/LibrariesExternalProject.txt) include(modules/InstallCDT.cmake) include(CTest) enable_testing() add_subdirectory(tests) ``` -------------------------------- ### Generate Skeleton Smart Contract Project with cdt-init Source: https://github.com/antelopeio/cdt/blob/main/docs/03_command-reference/cdt-init.md This bash command demonstrates how to use the cdt-init tool to generate a new smart contract project. It specifies the destination path and the project name. The tool creates a directory structure and a skeleton smart contract, optionally including CMake support. ```bash cdt-init --path=\/destination\/path\/where\/to\/generate\/project\/ --project=hello_contract_folder ``` -------------------------------- ### JSONPath Slice Operator Example Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/jsonpath/jsonpath.md Illustrates the array slice operator `[start:end:step]` for extracting a sub-sequence of elements from a JSON array, similar to slicing in Python. ```jsonpath $.store.book[0:2:1] ``` -------------------------------- ### WebAssembly Type Mismatch: i32 Atomic RMW16_U.XCHG Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/bad-atomic-type-mismatch.txt This example demonstrates a type mismatch where a function expects a 32-bit float (f32) but gets a 32-bit integer (i32). The error is attributed to the `i32.atomic.rmw16_u.xchg` instruction, indicating a type inconsistency. ```text out/test/typecheck/bad-atomic-type-mismatch.txt:197:46: error: type mismatch in implicit return, expected [f32] but got [i32] (func (result f32) i32.const 0 i32.const 0 i32.atomic.rmw16_u.xchg) ``` -------------------------------- ### WebAssembly Type Mismatch: i32 Atomic RMW8_U.XOR Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/bad-atomic-type-mismatch.txt This example shows a type mismatch where a function expects a 32-bit float (f32) but gets a 32-bit integer (i32). The error comes from the `i32.atomic.rmw8_u.xor` instruction, indicating a type conflict. ```text out/test/typecheck/bad-atomic-type-mismatch.txt:189:46: error: type mismatch in implicit return, expected [f32] but got [i32] (func (result f32) i32.const 0 i32.const 0 i32.atomic.rmw8_u.xor) ``` -------------------------------- ### WebAssembly Type Mismatch: i64 Atomic RMW32_U.XOR Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/bad-atomic-type-mismatch.txt This example highlights a type mismatch where a function expects a 64-bit float (f64) but gets a 64-bit integer (i64). The error is caused by the `i64.atomic.rmw32_u.xor` instruction, resulting in an incorrect return type. ```text out/test/typecheck/bad-atomic-type-mismatch.txt:193:46: error: type mismatch in implicit return, expected [f64] but got [i64] (func (result f64) i32.const 0 i64.const 0 i64.atomic.rmw32_u.xor) ``` -------------------------------- ### WebAssembly Type Mismatch: i64 Atomic RMW8_U.XOR Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/bad-atomic-type-mismatch.txt This example demonstrates a type mismatch where a function expects a 64-bit float (f64) but gets a 64-bit integer (i64). The error is tied to the `i64.atomic.rmw8_u.xor` instruction, indicating an incorrect return type. ```text out/test/typecheck/bad-atomic-type-mismatch.txt:191:46: error: type mismatch in implicit return, expected [f64] but got [i64] (func (result f64) i32.const 0 i64.const 0 i64.atomic.rmw8_u.xor) ``` -------------------------------- ### Get Help for wat2wasm Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command displays the help message for the `wat2wasm` tool, providing information on available options and usage. It's a standard way to discover the capabilities of a command-line utility. ```console $ out/wat2wasm -h ``` -------------------------------- ### WebAssembly Type Mismatch: i64 Atomic RMW16_U.OR Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/bad-atomic-type-mismatch.txt This example demonstrates a type mismatch where a function expects a 64-bit float (f64) but gets a 64-bit integer (i64). The error is attributed to the `i64.atomic.rmw16_u.or` instruction, indicating a return type mismatch. ```text out/test/typecheck/bad-atomic-type-mismatch.txt:185:46: error: type mismatch in implicit return, expected [f64] but got [i64] (func (result f64) i32.const 0 i64.const 0 i64.atomic.rmw16_u.or) ``` -------------------------------- ### CMake: Basic Project Setup and Versioning Source: https://github.com/antelopeio/cdt/blob/main/CMakeLists.txt Initializes the CMake build system, sets project name, defines version components, and constructs a full version string. It handles conditional appending of a version suffix. This is a foundational step for any CMake project. ```cmake cmake_minimum_required(VERSION 3.5...4.0) # Sanity check our source directory to make sure that we are not trying to # generate an in-source build, and to make # sure that we don't have any stray generated files lying around in the tree if( CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) message(FATAL_ERROR "In-source builds are not allowed. Please create a directory and run cmake from there, passing the path to this source directory as the last argument. This process created the file `CMakeCache.txt' and the directory `CMakeFiles'. Please delete them.") endif() project(cdt) set(VERSION_MAJOR 5) set(VERSION_MINOR 0) set(VERSION_PATCH 0) set(VERSION_SUFFIX "dev1") if (VERSION_SUFFIX) set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}") else() set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") endif() ``` -------------------------------- ### CMake ExternalProject_Add for toolchain-tester Source: https://github.com/antelopeio/cdt/blob/main/modules/ToolsExternalProject.txt Sets up the toolchain-tester external project within the CMake build system. It defines the source and binary directories and configures the build to always occur, while disabling update, patch, test, and install commands. ```cmake ExternalProject_Add( toolchain-tester SOURCE_DIR "${CMAKE_SOURCE_DIR}/tools/toolchain-tester" BINARY_DIR "${CMAKE_BINARY_DIR}/tools/toolchain-tester" UPDATE_COMMAND "" PATCH_COMMAND "" TEST_COMMAND "" INSTALL_COMMAND "" BUILD_ALWAYS 1 ) ``` -------------------------------- ### WebAssembly Type Mismatch: i64 Atomic RMW.XCHG Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/typecheck/bad-atomic-type-mismatch.txt This example shows a type mismatch where a function expects a 64-bit float (f64) but gets a 64-bit integer (i64). The error originates from the `i64.atomic.rmw.xchg` instruction, indicating a type conflict in the return value. ```text out/test/typecheck/bad-atomic-type-mismatch.txt:195:46: error: type mismatch in implicit return, expected [f64] but got [i64] (func (result f64) i32.const 0 i64.const 0 i64.atomic.rmw.xchg) ``` -------------------------------- ### run-interp Tool Execution Output Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/interp/brtable.txt This section shows the output of the run-interp tool when executing the provided WebAssembly module. It details the results of calling the exported test functions, demonstrating the behavior of the '$f' function with different inputs. ```text (;; STDOUT ;; test0() => i32:0 test1() => i32:1 test2() => i32:2 test3() => i32:2 ;;; STDOUT ;;; ``` -------------------------------- ### WebAssembly Module Execution with run-interp Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/interp/basic-logging.txt This snippet shows the execution of a WebAssembly module using the 'run-interp' tool. It defines a simple module that exports a 'main' function returning 42. The output includes the WASM binary representation and a textual description of the module's structure and execution flow. ```wast (module (func (export "main") (result i32) i32.const 42 return)) ) ``` ```text ;;; STDOUT ;%% 0000000: 0061 736d ; WASM_BINARY_MAGIC 0000004: 0100 0000 ; WASM_BINARY_VERSION ; section "Type" (1) 0000008: 01 ; section code 0000009: 00 ; section size (guess) 000000a: 01 ; num types ; type 0 000000b: 60 ; func 000000c: 00 ; num params 000000d: 01 ; num results 000000e: 7f ; i32 0000009: 05 ; FIXUP section size ; section "Function" (3) 000000f: 03 ; section code 0000010: 00 ; section size (guess) 0000011: 01 ; num functions 0000012: 00 ; function 0 signature index 0000010: 02 ; FIXUP section size ; section "Export" (7) 0000013: 07 ; section code 0000014: 00 ; section size (guess) 0000015: 01 ; num exports 0000016: 04 ; string length 0000017: 6d61 696e main ; export name 000001b: 00 ; export kind 000001c: 00 ; export func index 0000014: 08 ; FIXUP section size ; section "Code" (10) 000001d: 0a ; section code 000001e: 00 ; section size (guess) 000001f: 01 ; num functions ; function body 0 0000020: 00 ; func body size (guess) 0000021: 00 ; local decl count 0000022: 41 ; i32.const 0000023: 2a ; i32 literal 0000024: 0f ; return 0000025: 0b ; end 0000020: 05 ; FIXUP func body size 000001e: 07 ; FIXUP section size BeginModule(version: 1) BeginTypeSection(5) OnTypeCount(1) OnType(index: 0, params: [], results: [i32]) EndTypeSection BeginFunctionSection(2) OnFunctionCount(1) OnFunction(index: 0, sig_index: 0) EndFunctionSection BeginExportSection(8) OnExportCount(1) OnExport(index: 0, kind: func, item_index: 0, name: "main") EndExportSection BeginCodeSection(7) OnFunctionBodyCount(1) BeginFunctionBody(0) OnLocalDeclCount(0) OnI32ConstExpr(42 (0x2a)) OnReturnExpr EndFunctionBody(0) EndCodeSection EndModule 0| i32.const $42 5| return 6| return main() => i32:42 ;;; STDOUT ;;) ``` -------------------------------- ### run-gen-wasm Tool Arguments and Input Syntax Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/binary/user-section.txt Demonstrates the command-line arguments for the run-gen-wasm tool and the declarative syntax used to define WebAssembly modules, including sections, types, and function signatures. No external dependencies are required beyond the tool itself. ```text ;;; TOOL: run-gen-wasm ;;; ARGS: -v magic version section("foo") { count[4] } section(TYPE) { count[1] function params[0] results[1] i32 } section("bar") { count[5] } section("foo") { count[6] } ``` -------------------------------- ### Uninstall CDT (Manual Installation) Source: https://github.com/antelopeio/cdt/blob/main/README.md Removes CDT files and directories that were installed manually using 'make install'. This involves deleting specific directories and executable links from standard system locations. ```shell sudo rm -fr /usr/local/cdt sudo rm -fr /usr/local/lib/cmake/cdt sudo rm /usr/local/bin/eosio-* sudo rm /usr/local/bin/cdt-* ``` -------------------------------- ### Push 'regpkey' Action with Cleos Source: https://github.com/antelopeio/cdt/blob/main/docs/09_tutorials/01_binary-extension.md Shows how to use 'cleos push action' to execute the 'regpkey' action on the 'eosio' contract. This example registers a public key associated with 'eosio.name2', specifying 'eosio' as the payer. The output includes the transaction ID, execution time, and console logs from the contract. ```bash ~/binary_extension_contract $ cleos push action eosio regpkey '{"primary_key":"eosio.name2"}' -p eosio executed transaction: 75a135d1279a9c967078b0ebe337dc0cd58e1ccd07e370a899d9769391509afc 104 bytes 227 us # eosio <= eosio::regpkey {"primary_key":"eosio.name2"} [(eosio,regpkey)->eosio]: CONSOLE OUTPUT BEGIN ===================== `regpkey` executing. `_primary_key`: eosio.name2 not found; registering. `regpkey` finished executing. [(eosio,regpkey)->eosio]: CONSOLE OUTPUT END ===================== warning: transaction executed locally, but may not be confirmed by the network yet ``` -------------------------------- ### EOSIO: Print Primary Key Action (cleos) Source: https://github.com/antelopeio/cdt/blob/main/docs/05_features/30_binary-extension.md This command uses `cleos` to push the `printbyp` action to the `eosio` contract. It demonstrates the expected console output when the primary key is found and printed. This action is part of the EOSIO system contract. ```bash cleos push action eosio printbyp '{"primary_key":"eosio.name"}' -p eosio # eosio <= eosio::printbyp {"primary_key":"eosio.name"} [(eosio,printbyp)->eosio]: CONSOLE OUTPUT BEGIN ===================== `printbyp` executing. `_primary_key`: eosio.name found; printing. {eosio.name, nothin} `printbyp` finished executing. [(eosio,printbyp)->eosio]: CONSOLE OUTPUT END ===================== warning: transaction executed locally, but may not be confirmed by the network yet ``` -------------------------------- ### Build WABT on Windows using CMake Command Line Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md General command to configure a WABT build on Windows using CMake from the command line. Replace placeholders for build type, installation prefix, and generator. Requires CMake and a compatible C++ compiler (e.g., Visual Studio, MinGW). ```bash > cd [build dir] > cmake [wabt project root] -DCMAKE_BUILD_TYPE=[config] -DCMAKE_INSTALL_PREFIX=[install directory] -G [generator] ``` -------------------------------- ### Run Tests with Release Build and ASAN Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command runs project tests using a release build configuration and the Address Sanitizer (ASAN). This helps in identifying memory issues that might only appear in optimized or release builds. ```console $ make test-clang-release-asan ``` -------------------------------- ### WebAssembly: v128 SIMD Constant Declaration Examples Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/parse/expr/bad-const-v128-type-i32-expected.txt These examples demonstrate valid ways to declare v128 SIMD constants in WebAssembly text format. The first example uses a direct hexadecimal representation, while the second specifies the `i64` type for the constant, showcasing flexibility in defining SIMD values. ```wasm (module (func v128.const 0x12345678 0x00000000 0x00000000 0xabcd3478)) ``` ```wasm (module (func v128.const i64 0x12345678 0x00000000 0x00000000 0xabcd3478)) ``` -------------------------------- ### Get Help for wasm2wat Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command displays the help message for the `wasm2wat` tool, providing information on available options and usage. It's a standard way to discover the capabilities of a command-line utility. ```console $ out/wasm2wat -h ``` -------------------------------- ### Set and Get Data from Singleton (C++) Source: https://github.com/antelopeio/cdt/blob/main/docs/06_how-to-guides/40_multi-index/how-to-define-a-singleton.md Implements the `set` and `get` actions for the `singleton_example` contract. The `set` action uses `get_or_create` and `set` methods of the singleton to store or update data, with the `user` account paying for RAM. The `get` action checks if the singleton exists and prints the stored value, or indicates if it's empty. ```cpp #include [[eosio::action]] void singleton_example::set( name user, uint64_t value ) { auto entry_stored = singleton_instance.get_or_create(user, testtablerow); entry_stored.primary_value = user; entry_stored.secondary_value = value; singleton_instance.set(entry_stored, user); } [[eosio::action]] void singleton_example::get( ) { if (singleton_instance.exists()) eosio::print( "Value stored for: ", name{singleton_instance.get().primary_value.value}, " is ", singleton_instance.get().secondary_value, "\n"); else eosio::print("Singleton is empty\n"); } ``` -------------------------------- ### Run All Project Tests Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command executes the `test-everything` target, which runs all configured tests for the project. This is a convenient way to ensure all aspects of the project are functioning correctly before submitting changes. ```console $ make test-everything ``` -------------------------------- ### CMake: Configure jsoncons examples build Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/examples/build/cmake/CMakeLists.txt This snippet demonstrates the basic CMake configuration for the jsoncons examples. It sets the minimum required CMake version, includes global and platform-specific configuration files, defines the project, includes directories, finds source files, creates an executable target, and conditionally links libraries for specific platforms and compilers. ```cmake cmake_minimum_required (VERSION 2.8) # load global config include (../../../build/cmake/config.cmake) project (Examples CXX) # load per-platform configuration include (../../../build/cmake/${CMAKE_SYSTEM_NAME}.cmake) include_directories (../../include ../../../include) file(GLOB_RECURSE Example_sources ../../src/*.cpp) add_executable (jsoncons_examples ${Example_sources}) if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") # special link option on Linux because llvm stl rely on GNU stl target_link_libraries (jsoncons_examples -Wl,-lstdc++) endif() ``` -------------------------------- ### Deploy Smart Contract with cleos Source: https://github.com/antelopeio/cdt/blob/main/examples/multi_index_large/README.txt This command deploys a compiled smart contract to the blockchain. It requires creating an account first, then setting the contract with its ABI and WASM files. The '-p' flag specifies the payer account. ```shell cleos create account eosio testtaba your_public_key cleos set contract testtaba multi_index_large_wasm_directory multi_index_large.wasm multi_index_large.abi -p testtaba ``` -------------------------------- ### WebAssembly Module with Arithmetic and Indirect Calls Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/interp/callindirect.txt This WebAssembly module defines arithmetic operations (add, sub) and basic utility functions (zero, one). It includes a function table for indirect calls and exports test functions to demonstrate functionality and error conditions like out-of-bounds access and signature mismatches. The module is designed to be run with the `run-interp` tool. ```WebAssembly (module (type $v_i (func (result i32))) (func $zero (type $v_i) i32.const 0) (func $one (type $v_i) i32.const 1) (func $nullary (param i32) (result i32) get_local 0 call_indirect (type $v_i)) (type $ii_i (func (param i32 i32) (result i32))) (func $add (type $ii_i) get_local 0 get_local 1 i32.add) (func $sub (type $ii_i) get_local 0 get_local 1 i32.sub) (func $binary (param i32 i32 i32) (result i32) get_local 0 get_local 1 get_local 2 call_indirect (type $ii_i)) (table anyfunc (elem $zero $one $add $sub)) (func (export "test_zero") (result i32) i32.const 0 call $nullary) (func (export "test_one") (result i32) i32.const 1 call $nullary) (func (export "test_add") (result i32) i32.const 10 i32.const 4 i32.const 2 call $binary) (func (export "test_sub") (result i32) i32.const 10 i32.const 4 i32.const 3 call $binary) (func (export "trap_oob") (result i32) i32.const 10 i32.const 4 i32.const 4 call $binary) (func (export "trap_sig_mismatch") (result i32) i32.const 10 i32.const 4 i32.const 0 call $binary)) ``` -------------------------------- ### Generated WebAssembly Module Example Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/binary/names.txt This is an example of the WebAssembly module generated by the run-gen-wasm tool. It includes a function that takes no arguments and returns an i32, with a local i32 variable. ```wasm ;; STDOUT ;; (module $M0 (type (;0;) (func (result i32))) (func $F0 (type 0) (result i32) (local $L0 i32) get_local $L0)) ;;; STDOUT ;; ``` -------------------------------- ### EOSIO ABI Vector Definition Example Source: https://github.com/antelopeio/cdt/blob/main/docs/07_best-practices/08_abi/00_understanding-abi-files.md This snippet illustrates how to define a vector in an ABI file by appending '[]' to the type. For example, a vector of permission levels is represented as 'permission_level[]'. ```json "permission_level[]" ``` -------------------------------- ### Run Travis Build Script with Clang Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command executes the `travis-build.sh` script using Clang as the C compiler. This script is part of the Continuous Integration (CI) setup and mimics the build process used by Travis CI. ```shell $ CC=clang scripts/travis-build.sh ``` -------------------------------- ### Install Python Package for Pygments Source: https://github.com/antelopeio/cdt/blob/main/README.md Installs the Pygments library using pip, which is often used for code highlighting and may be a dependency for build or testing scripts. ```python python3 -m pip install pygments ``` -------------------------------- ### JSON Data Structure for Examples Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/jsonpath/jsonpath.md This is the sample JSON data structure used throughout the examples. It represents a bookstore with books and a bicycle, serving as the target for JSONPath queries. ```json { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } ``` -------------------------------- ### Execute 'printbyp' Action with Cleos Source: https://github.com/antelopeio/cdt/blob/main/docs/09_tutorials/01_binary-extension.md Demonstrates how to execute the 'printbyp' action on the 'eosio' contract using the 'cleos' command-line tool. It specifies the primary key as 'eosio.name' and the payer as 'eosio'. The output shows the contract's console logs indicating the action's execution and result. ```bash # eosio <= eosio::printbyp {"primary_key":"eosio.name"} [(eosio,printbyp)->eosio]: CONSOLE OUTPUT BEGIN ===================== `printbyp` executing. `_primary_key`: eosio.name found; printing. {eosio.name, nothin} `printbyp` finished executing. [(eosio,printbyp)->eosio]: CONSOLE OUTPUT END ===================== warning: transaction executed locally, but may not be confirmed by the network yet ``` -------------------------------- ### WebAssembly Basic Branching Test (run-interp) Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/interp/br.txt This snippet demonstrates a basic WebAssembly function 'br0' that uses 'if' and 'br' to control execution flow. It tests conditional branching and the introduction of blocks. ```wast (module ;; basic br test (func (export "br0") (result i32) (local i32 i32) block $exit i32.const 1 if br 1 ;; if branches introduce blocks end i32.const 1 set_local 0 ;; not executed end i32.const 1 set_local 1 get_local 0 i32.const 0 i32.eq get_local 1 i32.const 1 i32.eq i32.add return) ) ``` -------------------------------- ### JSONPath Filter Expression Example Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/jsonpath/jsonpath.md Shows how to use filter expressions enclosed in parentheses `()` to select elements based on specific conditions. This example filters books by price. ```jsonpath $.store.book[?(@.price < 10)] ``` -------------------------------- ### Valid wat2wasm Module Definitions Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/parse/expr/bad-const-type-i32-in-non-simd-const.txt These examples show basic, valid WebAssembly module definitions that can be processed by `wat2wasm`. They illustrate different numeric types for constant declarations. ```wat (module (func i32.const i32 100)) (module (func i64.const i32 100)) (module (func f32.const i32 100)) (module (func f64.const i32 100)) ``` -------------------------------- ### PowerPC Trampoline Setup Source: https://github.com/antelopeio/cdt/blob/main/libraries/rt/README.txt The `__trampoline_setup` function on PowerPC generates a custom trampoline function. It configures the trampoline with specific values for `realFunc` and `localsPtr`, following a standard template for dynamic function calls. ```c void __trampoline_setup(uint32_t* trampOnStack, int trampSizeAllocated, const void* realFunc, void* localsPtr); ``` -------------------------------- ### JSONPath Aggregate Function - Max Example Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/jsonpath/jsonpath.md Provides an example of using the `max()` aggregate function within a filter expression to find books with a price less than the maximum price among all books. ```jsonpath $.store.book[?(@.price < max($.store.book[*].price))].title ``` -------------------------------- ### Wasm-opcodecnt Command-Line Usage Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/help/wasm-opcodecnt.txt This snippet demonstrates the basic command-line usage of the wasm-opcodecnt tool, including options for help and output file specification. It requires a filename as input. ```bash # Display help message %(wasm-opcodecnt)s --help # Parse binary file test.wasm and write pcode dist file test.dist $ wasm-opcodecnt test.wasm -o test.dist ``` -------------------------------- ### C++ json Accessors and Defaults Example Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/json.md Demonstrates accessing json object members using `has_key` and `as`, and retrieving values with defaults using `get_with_default`. This example shows how to safely access potentially missing fields. ```c++ json val; val["field1"] = 1; val["field3"] = "Toronto"; double x1 = val.has_key("field1") ? val["field1"].as() : 10.0; double x2 = val.has_key("field2") ? val["field2"].as() : 20.0; std::string x3 = val.get_with_default("field3","Montreal"); std::string x4 = val.get_with_default("field4","San Francisco"); std::cout << "x1=" << x1 << '\n'; std::cout << "x2=" << x2 << '\n'; std::cout << "x3=" << x3 << '\n'; std::cout << "x4=" << x4 << '\n'; ``` -------------------------------- ### Get Help for wasm-interp Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command displays the help message for the `wasm-interp` tool, providing information on available options and usage. It's a standard way to discover the capabilities of a command-line utility. ```console $ out/wasm-interp -h ``` -------------------------------- ### C++ Incremental JSON Parsing with Unconsumed Characters Example Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/json_parser.md Illustrates incremental JSON parsing where unconsumed non-whitespace characters are present after the main JSON text. This example shows how `check_done()` detects and reports such errors. ```c++ int main() { jsoncons::json_decoder decoder; json_parser parser(decoder); try { parser.update("[10"); parser.parse_some(); std::cout << "(1) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n"; parser.update(".5]{}"); parser.parse_some(); // The parser reaches the end at ']' std::cout << "(2) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n"; parser.end_parse(); // Indicates that this is the end std::cout << "(3) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n"; parser.check_done(); // Checks if there are any unconsumed // non-whitespace characters in the input // (there are) } catch (const parse_error& e) { std::cout << "(4) " << e.what() << std::endl; } } ``` -------------------------------- ### Implement New Action with Conditional Logic (C++) Source: https://github.com/antelopeio/cdt/blob/main/docs/09_tutorials/01_binary-extension.md This C++ code snippet shows the implementation of the `regpkey` action in a smart contract. It includes logic to handle a new `secondary_key` parameter, assigning it or a default value if not provided. The code uses `eosio::print_f` for logging and demonstrates table operations like `get_index`, `find`, and `emplace`. ```cpp [[eosio::action]] void binary_extension_contract::regpkey(name primary_key, name secondary_key) { eosio::print_f("`regpkey` executing.\n"); auto index{_table.get_index<"index1"_n>()}; auto iter {index.find(primary_key.value) }; if (iter == _table.get_index<"index1"_n>().end()) { eosio::print_f("`_primary_key`: % not found; registering.\n", primary_key.to_string()); _table.emplace(_self, [&](auto& row) { row._primary_key = primary_key; + if (secondary_key) { + row._secondary_key = secondary_key; + } + else { row._secondary_key = "nothin"_n; + } }); } else { eosio::print_f("`_primary_key`: % found; not registering.\n", primary_key.to_string()); } eosio::print_f("`regpkey` finished executing.\n"); } ``` -------------------------------- ### Example: Parse JSON from String with Error Handling Source: https://github.com/antelopeio/cdt/blob/main/tools/jsoncons/doc/ref/json/parse.md Illustrates parsing a JSON string that contains an extra comma, triggering a parse_error. The example shows how to catch and handle this exception, printing the error message. ```C++ try { json val = json::parse("[1,2,3,4,]"); } catch(const jsoncons::parse_error& e) { std::cout << e.what() << std::endl; } ``` -------------------------------- ### ABI Vector Type Definition Example Source: https://github.com/antelopeio/cdt/blob/main/docs/09_tutorials/03_create-an-abi-file.md This example demonstrates how to define a vector type in an ABI file. By appending '[]' to a base type, you can specify an array or list of that type, such as a vector of permission levels. ```json { "name": "permissions", "type": "permission_level[]" } ``` -------------------------------- ### f64 Comparison Instructions in WebAssembly Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/test/dump/compare.txt This snippet illustrates WebAssembly instructions for comparing 64-bit floating-point numbers (f64). It covers equality (eq), inequality (ne), less than (lt), less than or equal to (le), greater than (gt), and greater than or equal to (ge). Each instruction is set up by pushing two f64 constants (0.0) onto the stack, followed by the comparison operation and a drop. ```wasm 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 61 | f64.eq 1a | drop 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 62 | f64.ne 1a | drop 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 63 | f64.lt 1a | drop 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 65 | f64.le 1a | drop 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 64 | f64.gt 1a | drop 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0 66 | f64.ge 1a | drop ``` -------------------------------- ### Run Travis Build Script with GCC Source: https://github.com/antelopeio/cdt/blob/main/tools/external/wabt/README.md This command executes the `travis-build.sh` script using GCC as the C compiler. This script is part of the Continuous Integration (CI) setup and mimics the build process used by Travis CI. ```shell $ CC=gcc scripts/travis-build.sh ``` -------------------------------- ### Build CDT from Source Source: https://github.com/antelopeio/cdt/blob/main/README.md Clones the CDT repository, configures the build using CMake, and compiles the project. It's recommended to be aware of potential memory issues with parallel compilation flags like '-j $(nproc)'. Binaries are placed in 'build/bin'. ```shell git clone --recursive https://github.com/AntelopeIO/cdt cd cdt mkdir build cd build cmake .. make -j $(nproc) ```