From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp32.i.mail.ru (smtp32.i.mail.ru [94.100.177.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 1D40E430407 for ; Fri, 21 Aug 2020 19:26:08 +0300 (MSK) From: olegrok@tarantool.org Date: Fri, 21 Aug 2020 19:26:07 +0300 Message-Id: <20200821162607.68179-1-olegrok@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH] exports: allow to use json tools via FFI List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tsafin@tarantool.org, v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org From: Oleg Babin This patch exports some json functions to be used via FFI. We solve following problem: currently we don't have any tools to inspect jsonpaths. E.g. user wants to ban or restrict an access to some tuple fields, it could be some system fields. Tarantool doesn't have hidden fields and to solve such problem we should fairly parse input jsonpaths. Before this patch user should write its own or some external tools. This patch allows to use functions from built-in json-lexer directly from Tarantool via FFI. Part of #5203 --- Issue: https://github.com/tarantool/tarantool/issues/5203 Branch: https://github.com/tarantool/tarantool/tree/olegrok/5203-expose-json-helpers Also issue contains an example of usage this feature. @Changelog: - Some symbols from tarantool json moudule are exported now (gh-5203). src/exports.h | 4 ++ test/box-tap/gh-5203-json-exports.test.lua | 82 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100755 test/box-tap/gh-5203-json-exports.test.lua diff --git a/src/exports.h b/src/exports.h index 7cf283e5b..83c90a8bc 100644 --- a/src/exports.h +++ b/src/exports.h @@ -152,6 +152,10 @@ EXPORT(ibuf_create) EXPORT(ibuf_destroy) EXPORT(ibuf_reinit) EXPORT(ibuf_reserve_slow) +EXPORT(json_lexer_next_token) +EXPORT(json_path_cmp) +EXPORT(json_path_validate) +EXPORT(json_path_multikey_offset) EXPORT(lbox_socket_local_resolve) EXPORT(lbox_socket_nonblock) EXPORT(log_format) diff --git a/test/box-tap/gh-5203-json-exports.test.lua b/test/box-tap/gh-5203-json-exports.test.lua new file mode 100755 index 000000000..1b8eb9afa --- /dev/null +++ b/test/box-tap/gh-5203-json-exports.test.lua @@ -0,0 +1,82 @@ +#!/usr/bin/env tarantool + +local tap = require('tap') +local ffi = require('ffi') +ffi.cdef([[ + void *dlsym(void *handle, const char *symbol); + /** + * Lexer for JSON paths: + * , <.field>, <[123]>, <['field']> and their combinations. + */ + struct json_lexer { + /** Source string. */ + const char *src; + /** Length of string. */ + int src_len; + /** Current lexer's offset in bytes. */ + int offset; + /** Current lexer's offset in symbols. */ + int symbol_count; + /** + * Base field offset for emitted JSON_TOKEN_NUM tokens, + * e.g. 0 for C and 1 for Lua. + */ + int index_base; + }; + + enum json_token_type { + JSON_TOKEN_NUM, + JSON_TOKEN_STR, + JSON_TOKEN_ANY, + /** Lexer reached end of path. */ + JSON_TOKEN_END, + }; + + int + json_lexer_next_token(struct json_lexer *lexer, struct json_token *token); + + int + json_path_cmp(const char *a, int a_len, const char *b, int b_len, + int index_base); + + int + json_path_validate(const char *path, int path_len, int index_base); + + int + json_path_multikey_offset(const char *path, int path_len, int index_base); +]]) + +local test = tap.test('json-features') +test:plan(1) + +local RTLD_DEFAULT +-- See `man 3 dlsym`: +-- RTLD_DEFAULT +-- Find the first occurrence of the desired symbol using the default +-- shared object search order. The search will include global symbols +-- in the executable and its dependencies, as well as symbols in shared +-- objects that were dynamically loaded with the RTLD_GLOBAL flag. +if jit.os == "OSX" then + RTLD_DEFAULT = ffi.cast("void *", -2LL) +else + RTLD_DEFAULT = ffi.cast("void *", 0LL) +end + +local json_symbols = { + 'json_lexer_next_token', + 'json_path_cmp', + 'json_path_validate', + 'json_path_multikey_offset', +} + +test:test('json_symbols', function(t) + t:plan(#json_symbols) + for _, sym in ipairs(json_symbols) do + t:ok( + ffi.C.dlsym(RTLD_DEFAULT, sym) ~= nil, + ('Symbol %q found'):format(sym) + ) + end +end) + +os.exit(test:check() and 0 or 1) -- 2.23.0