Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument float bug #123

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/liquid_c/lexer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "liquid.h"
#include "lexer.h"
#include "usage.h"
#include <stdio.h>

const char *symbol_names[TOKEN_END] = {
Expand Down Expand Up @@ -107,6 +108,11 @@ const char *lex_one(const char *start, const char *end, lexer_token_t *token)
}
}

// Instrument for bug: https://github.com/Shopify/liquid-c/pull/120
if (c == '-' && str + 1 < end && str[1] == '.') {
usage_increment("liquid_c_negative_float_without_integer");
}

if (ISDIGIT(c) || c == '-') {
int has_dot = 0;
cur = str;
Expand Down
2 changes: 2 additions & 0 deletions ext/liquid_c/liquid.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "context.h"
#include "variable_lookup.h"
#include "vm.h"
#include "usage.h"

ID id_evaluate;
ID id_to_liquid;
Expand Down Expand Up @@ -82,5 +83,6 @@ void Init_liquid_c(void)
init_liquid_context();
init_liquid_variable_lookup();
init_liquid_vm();
init_liquid_usage();
}

18 changes: 18 additions & 0 deletions ext/liquid_c/usage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "usage.h"

static VALUE cLiquidUsage;
static ID id_increment;

void usage_increment(const char *name)
{
VALUE name_str = rb_str_new_cstr(name);
rb_funcall(cLiquidUsage, id_increment, 1, name_str);
}

void init_liquid_usage()
{
cLiquidUsage = rb_const_get(mLiquid, rb_intern("Usage"));
rb_global_variable(&cLiquidUsage);

id_increment = rb_intern("increment");
}
9 changes: 9 additions & 0 deletions ext/liquid_c/usage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef LIQUID_USAGE_H
#define LIQUID_USAGE_H

#include "liquid.h"

void init_liquid_usage();
void usage_increment(const char *name);

#endif
15 changes: 15 additions & 0 deletions test/unit/block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def test_op_write_raw_w
assert_equal(source, template.render!)
end

# Test for bug: https://github.com/Shopify/liquid-c/pull/120
def test_bug_120_instrument
calls = []
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
Liquid::Template.parse("{{ -.1 }}")
end
assert_equal(["liquid_c_negative_float_without_integer"], calls)

calls = []
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
Liquid::Template.parse("{{ .1 }}")
end
assert_equal([], calls)
end

def test_disassemble_raw_w
source = "a" * 2**8
template = Liquid::Template.parse(source)
Expand Down