Skip to content

Commit 8a9a049

Browse files
authored
Merge pull request #123 from Shopify/pz-instrument-float-bug
Instrument float bug
2 parents 9cb42ee + da97e3c commit 8a9a049

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

ext/liquid_c/lexer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "liquid.h"
22
#include "lexer.h"
3+
#include "usage.h"
34
#include <stdio.h>
45

56
const char *symbol_names[TOKEN_END] = {
@@ -107,6 +108,11 @@ const char *lex_one(const char *start, const char *end, lexer_token_t *token)
107108
}
108109
}
109110

111+
// Instrument for bug: https://github.com/Shopify/liquid-c/pull/120
112+
if (c == '-' && str + 1 < end && str[1] == '.') {
113+
usage_increment("liquid_c_negative_float_without_integer");
114+
}
115+
110116
if (ISDIGIT(c) || c == '-') {
111117
int has_dot = 0;
112118
cur = str;

ext/liquid_c/liquid.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "context.h"
1111
#include "variable_lookup.h"
1212
#include "vm.h"
13+
#include "usage.h"
1314

1415
ID id_evaluate;
1516
ID id_to_liquid;
@@ -82,5 +83,6 @@ void Init_liquid_c(void)
8283
init_liquid_context();
8384
init_liquid_variable_lookup();
8485
init_liquid_vm();
86+
init_liquid_usage();
8587
}
8688

ext/liquid_c/usage.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "usage.h"
2+
3+
static VALUE cLiquidUsage;
4+
static ID id_increment;
5+
6+
void usage_increment(const char *name)
7+
{
8+
VALUE name_str = rb_str_new_cstr(name);
9+
rb_funcall(cLiquidUsage, id_increment, 1, name_str);
10+
}
11+
12+
void init_liquid_usage()
13+
{
14+
cLiquidUsage = rb_const_get(mLiquid, rb_intern("Usage"));
15+
rb_global_variable(&cLiquidUsage);
16+
17+
id_increment = rb_intern("increment");
18+
}

ext/liquid_c/usage.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef LIQUID_USAGE_H
2+
#define LIQUID_USAGE_H
3+
4+
#include "liquid.h"
5+
6+
void init_liquid_usage();
7+
void usage_increment(const char *name);
8+
9+
#endif

test/unit/block_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ def test_op_write_raw_w
3131
assert_equal(source, template.render!)
3232
end
3333

34+
# Test for bug: https://github.com/Shopify/liquid-c/pull/120
35+
def test_bug_120_instrument
36+
calls = []
37+
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
38+
Liquid::Template.parse("{{ -.1 }}")
39+
end
40+
assert_equal(["liquid_c_negative_float_without_integer"], calls)
41+
42+
calls = []
43+
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
44+
Liquid::Template.parse("{{ .1 }}")
45+
end
46+
assert_equal([], calls)
47+
end
48+
3449
def test_disassemble_raw_w
3550
source = "a" * 2**8
3651
template = Liquid::Template.parse(source)

0 commit comments

Comments
 (0)