jscript: Replace only the first match for non-regexp pattern in String.replace.
[wine/multimedia.git] / dlls / jscript / number.c
blobde0ff6307ff37537a09aae69871aee27be3e5c40
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 typedef struct {
31 jsdisp_t dispex;
33 double value;
34 } NumberInstance;
36 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
37 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
38 static const WCHAR toFixedW[] = {'t','o','F','i','x','e','d',0};
39 static const WCHAR toExponentialW[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
40 static const WCHAR toPrecisionW[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
41 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
43 #define NUMBER_TOSTRING_BUF_SIZE 64
45 static inline NumberInstance *number_from_vdisp(vdisp_t *vdisp)
47 return (NumberInstance*)vdisp->u.jsdisp;
50 static inline NumberInstance *number_this(vdisp_t *jsthis)
52 return is_vclass(jsthis, JSCLASS_NUMBER) ? number_from_vdisp(jsthis) : NULL;
55 /* ECMA-262 3rd Edition 15.7.4.2 */
56 static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
57 VARIANT *retv, jsexcept_t *ei)
59 NumberInstance *number;
60 INT radix = 10;
61 DOUBLE val;
62 BSTR str;
63 HRESULT hres;
65 TRACE("\n");
67 if(!(number = number_this(jsthis)))
68 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
70 if(arg_cnt(dp)) {
71 hres = to_int32(ctx, get_arg(dp, 0), ei, &radix);
72 if(FAILED(hres))
73 return hres;
75 if(radix<2 || radix>36)
76 return throw_type_error(ctx, ei, JS_E_INVALIDARG, NULL);
79 val = number->value;
81 if(radix==10 || isnan(val) || isinf(val)) {
82 VARIANT v;
84 num_set_val(&v, val);
85 hres = to_string(ctx, &v, ei, &str);
86 if(FAILED(hres))
87 return hres;
88 }else {
89 INT idx = 0;
90 DOUBLE integ, frac, log_radix = 0;
91 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
92 BOOL exp = FALSE;
94 if(val<0) {
95 val = -val;
96 buf[idx++] = '-';
99 while(1) {
100 integ = floor(val);
101 frac = val-integ;
103 if(integ == 0)
104 buf[idx++] = '0';
105 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
106 buf[idx] = fmod(integ, radix);
107 if(buf[idx]<10) buf[idx] += '0';
108 else buf[idx] += 'a'-10;
109 integ /= radix;
110 idx++;
113 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
114 INT beg = buf[0]=='-'?1:0;
115 INT end = idx-1;
116 WCHAR wch;
118 while(end > beg) {
119 wch = buf[beg];
120 buf[beg++] = buf[end];
121 buf[end--] = wch;
125 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
127 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
128 frac *= radix;
129 buf[idx] = fmod(frac, radix);
130 frac -= buf[idx];
131 if(buf[idx]<10) buf[idx] += '0';
132 else buf[idx] += 'a'-10;
133 idx++;
136 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
137 exp = TRUE;
138 idx = (buf[0]=='-') ? 1 : 0;
139 log_radix = floor(log(val)/log(radix));
140 val *= pow(radix, -log_radix);
141 continue;
144 break;
147 while(buf[idx-1] == '0') idx--;
148 if(buf[idx-1] == '.') idx--;
150 if(exp) {
151 if(log_radix==0)
152 buf[idx] = 0;
153 else {
154 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
155 WCHAR ch;
157 if(log_radix<0) {
158 log_radix = -log_radix;
159 ch = '-';
161 else ch = '+';
162 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
165 else buf[idx] = '\0';
167 str = SysAllocString(buf);
168 if(!str)
169 return E_OUTOFMEMORY;
172 if(retv) {
173 V_VT(retv) = VT_BSTR;
174 V_BSTR(retv) = str;
175 }else {
176 SysFreeString(str);
178 return S_OK;
181 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei)
184 FIXME("\n");
185 return E_NOTIMPL;
188 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
189 VARIANT *retv, jsexcept_t *ei)
191 FIXME("\n");
192 return E_NOTIMPL;
195 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
196 VARIANT *retv, jsexcept_t *ei)
198 FIXME("\n");
199 return E_NOTIMPL;
202 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
203 VARIANT *retv, jsexcept_t *ei)
205 FIXME("\n");
206 return E_NOTIMPL;
209 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
210 VARIANT *retv, jsexcept_t *ei)
212 NumberInstance *number;
214 TRACE("\n");
216 if(!(number = number_this(jsthis)))
217 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
219 if(retv)
220 num_set_val(retv, number->value);
221 return S_OK;
224 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei)
227 NumberInstance *number = number_from_vdisp(jsthis);
229 switch(flags) {
230 case INVOKE_FUNC:
231 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
232 case DISPATCH_PROPERTYGET:
233 num_set_val(retv, number->value);
234 break;
236 default:
237 FIXME("flags %x\n", flags);
238 return E_NOTIMPL;
241 return S_OK;
244 static const builtin_prop_t Number_props[] = {
245 {toExponentialW, Number_toExponential, PROPF_METHOD|1},
246 {toFixedW, Number_toFixed, PROPF_METHOD},
247 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
248 {toPrecisionW, Number_toPrecision, PROPF_METHOD|1},
249 {toStringW, Number_toString, PROPF_METHOD|1},
250 {valueOfW, Number_valueOf, PROPF_METHOD}
253 static const builtin_info_t Number_info = {
254 JSCLASS_NUMBER,
255 {NULL, Number_value, 0},
256 sizeof(Number_props)/sizeof(*Number_props),
257 Number_props,
258 NULL,
259 NULL
262 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
263 VARIANT *retv, jsexcept_t *ei)
265 double n;
266 HRESULT hres;
268 TRACE("\n");
270 switch(flags) {
271 case INVOKE_FUNC:
272 if(!arg_cnt(dp)) {
273 if(retv) {
274 V_VT(retv) = VT_I4;
275 V_I4(retv) = 0;
277 return S_OK;
280 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
281 if(FAILED(hres))
282 return hres;
284 if(retv)
285 num_set_val(retv, n);
286 break;
288 case DISPATCH_CONSTRUCT: {
289 jsdisp_t *obj;
291 if(arg_cnt(dp)) {
292 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
293 if(FAILED(hres))
294 return hres;
295 }else {
296 n = 0;
299 hres = create_number(ctx, n, &obj);
300 if(FAILED(hres))
301 return hres;
303 var_set_jsdisp(retv, obj);
304 break;
306 default:
307 FIXME("unimplemented flags %x\n", flags);
308 return E_NOTIMPL;
311 return S_OK;
314 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
316 NumberInstance *number;
317 HRESULT hres;
319 number = heap_alloc_zero(sizeof(NumberInstance));
320 if(!number)
321 return E_OUTOFMEMORY;
323 if(object_prototype)
324 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
325 else
326 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
327 if(FAILED(hres))
328 return hres;
330 *ret = number;
331 return S_OK;
334 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
336 NumberInstance *number;
337 HRESULT hres;
339 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
341 hres = alloc_number(ctx, object_prototype, &number);
342 if(FAILED(hres))
343 return hres;
345 number->value = 0;
346 hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL,
347 PROPF_CONSTR|1, &number->dispex, ret);
349 jsdisp_release(&number->dispex);
350 return hres;
353 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
355 NumberInstance *number;
356 HRESULT hres;
358 hres = alloc_number(ctx, NULL, &number);
359 if(FAILED(hres))
360 return hres;
362 number->value = value;
364 *ret = &number->dispex;
365 return S_OK;